無涯教程: Laravel 8 - Pagination分頁介紹


在此示例中,將開始向您說明如何使用laravel分頁。

步驟1:添加路由

首先,我們為具有分頁函數的列表用戶設置路由。

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\UserController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('users', [UserController::class, 'index']);

步驟2:創建控制器

與上述路由相同,這里我們將為路由添加一種新方法。 index()將返回帶有分頁數據,因此讓我們添加以下內容:

app/Http/Controllers/UserController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = User::paginate(5);
        return view('users',compact('data'));
    }
}

步驟3:創建視圖

在此步驟中,您需要創建用戶視圖blade文件,並將與links()一起使用,以便它將自動生成分頁。

resources/views/users.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 8 CRUD Application - 無涯教程</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
    
<div class="container">
    <h1>Laravel 8 Pagination Example - 無涯教程</h1>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Name</th>
                <th width="300px;">Action</th>
            </tr>
        </thead>
        <tbody>
            @if(!empty($data) && $data->count())
                @foreach($data as $key => $value)
                    <tr>
                        <td>{{ $value->name }}</td>
                        <td>
                            <button class="btn btn-danger">Delete</button>
                        </td>
                    </tr>
                @endforeach
            @else
                <tr>
                    <td colspan="10">There are no data.</td>
                </tr>
            @endif
        </tbody>
    </table>
         
    {!! $data->links() !!}
</div>
     
</body>
</html>

現在您可以運行並檢查此示例。這是一個非常簡單和基本的例子。

如果使用bootstrap程序,則必須在服務提供者(service provider)上添加useBootstrap(),如下所示:

app\Providers\AppServiceProvider.php

<?php
  
namespace App\Providers;
  
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;
  
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
    
    }
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Paginator::useBootstrap();
    }
}

如果您需要預先使用分頁,則可以在下面看到如何使用。

Pagination與appends parameter

{!! $data->appends(['sort' => 'votes'])->links() !!}

 

與追加分頁請求所有參數

{!! $data->appends(Request::all())->links() !!}

https://www.learnfk.com/article-laravel-8-pagination-example-tutorialexample


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM