怎么在Laravel中實(shí)現(xiàn)一個(gè)手動(dòng)分頁(yè)功能?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
創(chuàng)新互聯(lián)建站長(zhǎng)期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為霸州企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站,霸州網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。#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() }}
好了,基本就是這樣,有紕漏的地方歡迎指正!
看看最終效果:
看完上述內(nèi)容,你們掌握怎么在Laravel中實(shí)現(xiàn)一個(gè)手動(dòng)分頁(yè)功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當(dāng)前名稱:怎么在Laravel中實(shí)現(xiàn)一個(gè)手動(dòng)分頁(yè)功能-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://www.rwnh.cn/article2/ceceoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、靜態(tài)網(wǎng)站、ChatGPT、網(wǎng)站維護(hù)、網(wǎng)站建設(shè)、軟件開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容