Laravel是一款受歡迎的PHP開發框架,提供了許多現代化的特性和便於開發的工具,讓開發者可以更有效率地建立網站和應用程式。在開發Web應用過程中,常常需要將查詢結果分頁展示,這篇文章將介紹如何使用Laravel實現分頁功能。
一、分頁簡介
分頁是指將一份大量數據拆分成多個頁面進行展示的方法,通常情況下我們會設定顯示每頁多少條數據,數據量超過該數值便會自動翻頁。這種方式可以大幅減少頁面的載入時間和佔用頻寬,讓使用者更容易查看和導航資料。
二、Laravel分頁操作
Laravel提供了Paginator類別來實現分頁功能。我們可以將查詢結果物件傳遞給Paginator的make方法,並指定每頁要顯示的資料量,就可以取得分頁實例。在實例上呼叫render方法,Laravel即可自動生成分頁連結。
下面我們來看具體的程式碼實作。
執行以下指令安裝Laravel:
composer create-project --prefer-dist laravel/laravel blog
CREATE TABLE `articles` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `body` text COLLATE utf8mb4_unicode_ci NOT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `articles` (`title`, `body`, `created_at`, `updated_at`) VALUES ('Article 1', 'This is article 1 content', '2021-01-01 12:00:00', '2021-01-01 12:00:00'); INSERT INTO `articles` (`title`, `body`, `created_at`, `updated_at`) VALUES ('Article 2', 'This is article 2 content', '2021-01-02 12:00:00', '2021-01-02 12:00:00'); INSERT INTO `articles` (`title`, `body`, `created_at`, `updated_at`) VALUES ('Article 3', 'This is article 3 content', '2021-01-03 12:00:00', '2021-01-03 12:00:00'); INSERT INTO `articles` (`title`, `body`, `created_at`, `updated_at`) VALUES ('Article 4', 'This is article 4 content', '2021-01-04 12:00:00', '2021-01-04 12:00:00');
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Article extends Model { use HasFactory; protected $fillable = ['title', 'body']; public function getDateAttribute() { return $this->created_at->format('Y-m-d'); } }
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Article; class ArticlesController extends Controller { public function index(Request $request) { $articles = Article::paginate(2); return view('articles.index', compact('articles')); } }
@extends('layouts.app') @section('content') <div class="container"> <div class="row"> <div class="col-md-8"> @foreach($articles as $article) <div class="card mb-3"> <div class="card-header"> {{ $article->title }} </div> <div class="card-body"> <p class="card-text">{{ $article->body }}</p> </div> <div class="card-footer text-right"> <small class="text-muted">{{ $article->date }}</small> </div> </div> @endforeach {{ $articles->links() }} </div> </div> </div> @endsection
.card { margin-bottom: 20px; } .pagination { margin-top: 20px; }
<!doctype html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <!-- Meta Tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>{{ config('app.name') }}</title> <!-- Styles --> <link href="{{ asset('css/app.css') }}" rel="stylesheet"> </head> <body> <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm"> <div class="container"> <a class="navbar-brand" href="{{ url('/') }}"> {{ config('app.name') }} </a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"></ul> <ul class="navbar-nav ml-auto"></ul> </div> </div> </nav> @yield('content') <script src="{{ asset('js/app.js') }}"></script> </body> </html>
以上是如何使用Laravel實現分頁功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!