簡介
在其他框架中,分頁可能是件非常痛苦的事,Laravel 讓這件事變得簡單、易於上手。Laravel 的分頁器與查詢構建器和 Eloquent ORM 集成在一起,並開箱提供方便的、易於使用的、基於數據庫結果集的分頁。分頁器生成的 HTML 兼容 Bootstrap CSS 框架。
基本使用
基於查詢構建器進行分頁
有多種方式實現分頁功能,最簡單的方式就是使用查詢構建器或 Eloquent 查詢提供的 paginate
方法。該方法基於當前用戶查看頁自動設置合適的偏移(offset)和限制(limit),直白點說就是頁碼和每頁顯示數量。默認情況下,當前頁通過 HTTP 請求查詢字符串參數 page
的值判斷。當然,該值由 Laravel 自動檢測,然后自動插入分頁器生成的鏈接中。
讓我們先來看看如何在查詢中調用 paginate
方法。在本例中,傳遞給 paginate
的唯一參數就是你每頁想要顯示的數目,這里我們指定每頁顯示 15
個:
<?php namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; use App\Http\Controllers\Controller; class UserController extends Controller { /** * 顯示應用中的所有用戶 * * @return Response */ public function index() { $users = DB::table('users')->paginate(15); return view('user.index', ['users' => $users]); } }
注:目前,使用 groupBy
的分頁操作不能被 Laravel 有效執行,如果你需要在分頁結果中使用 groupBy
,推薦你手動查詢數據庫然后創建分頁器。
1.自定義分頁實現
手動創建分頁器
有時候你可能想要通過傳遞數組數據來手動創建分頁實例,你可以基於自己的需求通過創建 Illuminate\Pagination\Paginator
或 Illuminate\Pagination\LengthAwarePaginator
實例來實現。
Paginator
類不需要知道結果集中數據項的總數;不過,正因如此,該類也沒有提供獲取最后一頁索引的方法。LengthAwarePaginator
接收參數和 Paginator
幾乎一樣,唯一不同在於它要求傳入結果集的總數。
換句話說,Paginator
對應 simplePaginate
方法,而 LengthAwarePaginator
對應 paginate
方法。
注:當手動創建分頁器實例的時候,應該手動對傳遞到分頁器的結果集進行“切片”,如果你不確定怎么做,查看 PHP 函數 array_slice。
Model類的 UserModel.php
public function getList() {//獲取用戶列表 $perPage =3;//每頁顯示幾條 $currentPage = 3;//當前第幾頁 $currentNum = $perPage*$currentPage;//從哪里開始產找數據 $results = DB::select('select * from user where 1 limit ?,?',[$currentNum,$perPage]); $total = DB::select('select COUNT(id) as id from user where 1 ');//數據總數 $total = $total[0]; $total = $total->id; //自定義分頁類 $result = new \Illuminate\Pagination\LengthAwarePaginator($results,$total,$perPage,$currentPage,['ff=ff&ee=ee','dd=dd']); $result->setPath("?pa=sf&sdf=sdf&sdkf=sdf");//如果有搜索傳遞參數 return $result; }
控制器的 UserController.php
public function userList() { $userModel = new UserModel(); $res = $userModel->getList(); var_dump(htmlspecialchars($res->render()));//打印分頁 var_dump($res->render());//打印分頁 dd($res);//打印結果並終止 return $res; }
$res->links()也可以打印分頁信息
結果:
下來我看自定義的分頁樣式怎么弄(好像很簡單,只要把樣式模板導出來修改就可以了,怎么導出來了,看下面介紹):
自定義分頁視圖
默認情況下,用於渲染分頁鏈接的視圖兼容 Bootstrap CSS 框架,如果你沒有使用Bootstrap,可以自定義視圖來渲染這些鏈接。當調用分頁器實例上的 links
方法時,傳遞視圖名稱作為第一個參數:
{{ $paginator->links('view.name') }} // 傳遞數據到視圖... {{ $paginator->links('view.name', ['foo' => 'bar']) }}
不過,自定義分頁視圖最簡單的方式是使用 vendor:publish
命令導出視圖文件到resources/views/vendor
目錄:
php artisan vendor:publish --tag=laravel-pagination
該命令會將視圖放到 resources/views/vendor/pagination
目錄,該目錄下的default.blade.php
文件對應默認的視圖文件,編輯該文件即可修改分頁 HTML。
如果你想要指定其他文件作為默認分頁視圖,可以在 AppServiceProvider
中使用分頁器的 defaultView
和 defaultSimpleView
方法:
use Illuminate\Pagination\Paginator; public function boot() { Paginator::defaultView('pagination::view'); Paginator::defaultSimpleView('pagination::view'); }
上面的理解:
換一個分頁模板--
分頁器實例方法
每個分頁器實例都可以通過以下方法提供更多分頁信息:
$results->count() $results->currentPage() $results->firstItem() $results->hasMorePages() $results->lastItem() $results->lastPage() (使用simplePaginate 時無效) $results->nextPageUrl() $results->perPage() $results->previousPageUrl() $results->total() (使用simplePaginate 時無效) $results->url($page)
轉:http://laravelacademy.org/post/8841.html