Laravel手動(dòng)分頁(yè)實(shí)現(xiàn)方法詳解
本文實(shí)例講述了Laravel手動(dòng)分頁(yè)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
這里的演示實(shí)例基于Laravel的5.2版本
在開(kāi)發(fā)過(guò)程中有這么一種情況,你請(qǐng)求Java api獲取信息,由于信息較多,需要分頁(yè)顯示。Laravel官方提供了一個(gè)簡(jiǎn)單的方式paginate($perPage),但是這種方法只適用model、查詢構(gòu)建器。
今天說(shuō)下 給定一個(gè)數(shù)組如何實(shí)現(xiàn) 和paginate方法一樣的效果。
查看paginate方法源碼
#vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:480 public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) { $query = $this->toBase(); $total = $query->getCountForPagination(); $this->forPage( $page = $page ?: Paginator::resolveCurrentPage($pageName), $perPage = $perPage ?: $this->model->getPerPage() ); return new LengthAwarePaginator($this->get($columns), $total, $perPage, $page, [ 'path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName, ]); }
從上面就可以看出,分頁(yè)的關(guān)鍵就在于LengthAwarePaginator。
LengthAwarePaginator的構(gòu)造方法。
public function __construct($items, $total, $perPage, $currentPage = null, array $options = []) { foreach ($options as $key => $value) { $this->{$key} = $value; } $this->total = $total; $this->perPage = $perPage; $this->lastPage = (int) ceil($total / $perPage); $this->path = $this->path != '/' ? rtrim($this->path, '/') : $this->path; $this->currentPage = $this->setCurrentPage($currentPage, $this->lastPage); $this->items = $items instanceof Collection ? $items : Collection::make($items); }
其實(shí)已經(jīng)很明白了,假如要分頁(yè)的數(shù)組為
[ ['username'=>'zhangsan', 'age'=>26], ['username'=>'lisi', 'age'=>23], ['username'=>'wangwu', 'age'=>62], ['username'=>'zhaoliu', 'age'=>46], ['username'=>'wangmazi', 'age'=>25], ['username'=>'lanzi', 'age'=>24], ['username'=>'pangzi', 'age'=>21], ]
共7條數(shù)據(jù),每頁(yè)顯示3條,共3頁(yè)
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; use Illuminate\Http\Request; # 僅做演示 # function userList(Request $request) { $users = [ ['username'=>'zhangsan', 'age'=>26], ['username'=>'lisi', 'age'=>23], ['username'=>'wangwu', 'age'=>62], ['username'=>'zhaoliu', 'age'=>46], ['username'=>'wangmazi', 'age'=>25], ['username'=>'lanzi', 'age'=>24], ['username'=>'pangzi', 'age'=>21] ]; $perPage = 3; if ($request->has('page')) { $current_page = $request->input('page'); $current_page = $current_page <= 0 ? 1 :$current_page; } else { $current_page = 1; } $item = array_slice($users, ($current_page-1)*$perPage, $perPage); //注釋1 $total = count($users); $paginator =new LengthAwarePaginator($item, $total, $perPage, $currentPage, [ 'path' => Paginator::resolveCurrentPath(), //注釋2 'pageName' => 'page', ]); $userlist = $paginator->toArray()['data']; return view('userlist', compact('userlist', 'paginator')); }
上面的代碼中的重點(diǎn)是$item,如果不做注釋1處理,得出的是所有7條數(shù)據(jù)。
注釋2處就是設(shè)定個(gè)要分頁(yè)的url地址。也可以手動(dòng)通過(guò) $paginator ->setPath('路徑') 設(shè)置。
頁(yè)面中的分頁(yè)連接也是同樣的調(diào)用方式:
{{ $paginator->render() }}
好了,基本就是這樣,有紕漏的地方歡迎指正!
看看最終效果:
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
- Laravel5.5 手動(dòng)分頁(yè)和自定義分頁(yè)樣式的簡(jiǎn)單實(shí)現(xiàn)
- PHP框架Laravel插件Pagination實(shí)現(xiàn)自定義分頁(yè)
- laravel自定義分頁(yè)效果
- laravel自定義分頁(yè)的實(shí)現(xiàn)案例offset()和limit()
- laravel實(shí)現(xiàn)分頁(yè)樣式替換示例代碼(增加首、尾頁(yè))
- Laravel+jQuery實(shí)現(xiàn)AJAX分頁(yè)效果
- Laravel框架執(zhí)行原生SQL語(yǔ)句及使用paginate分頁(yè)的方法
- laravel手動(dòng)創(chuàng)建數(shù)組分頁(yè)的實(shí)現(xiàn)代碼
- Laravel框架自定義分頁(yè)樣式操作示例
相關(guān)文章
Yii2 rbac權(quán)限控制操作步驟實(shí)例教程
這篇文章主要介紹了Yii2 rbac權(quán)限控制操作步驟實(shí)例教程的相關(guān)資料,需要的朋友可以參考下2016-04-04PHP中常見(jiàn)的錯(cuò)誤與異常處理總結(jié)大全
任何程序員在開(kāi)發(fā)時(shí)都可能遇到過(guò)一些失誤,或其他原因造成錯(cuò)誤的發(fā)生。當(dāng)然,用戶如果不愿意或不遵循應(yīng)用程序的約束,也會(huì)在使用時(shí)引起一些錯(cuò)誤發(fā)生。下面這篇文章主要給大家介紹了關(guān)于PHP中常見(jiàn)的錯(cuò)誤與異常處理,需要的朋友可以參考下,2017-08-08Laravel中ServiceProvider使用場(chǎng)景示例詳解
這篇文章主要為大家介紹了Laravel中ServiceProvider使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06實(shí)例介紹PHP刪除數(shù)組中的重復(fù)元素
在本文里小編給大家分享了關(guān)于PHP如何刪除數(shù)組中的重復(fù)元素的相關(guān)知識(shí)點(diǎn)和步驟,需要的朋友們學(xué)習(xí)下。2019-03-03