引入分页类:
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator;
获取当前访问页数和设置每页数量:
if ($request->has('page')) { $page = $request->input('page'); $page = $page <= 0 ? 1 : $page ;//当前要访问的页数 }
$perpage=10;//每页数量
对查询结果做切分:
$total=count($result);//结果集总数 $result=array_slice(结果集数组,($page-1)*$perpage(即数据起始位置,从0开始),每页数量,true);
调用手动分页类:
$paginator = new LengthAwarePaginator($result, $total, $perpage, $page, [ 'path' => Paginator::resolveCurrentPath(), 'pageName' => 'page', ]); $result= $paginator->toArray()['data'];
返回处理后结果集和分页给页面:
return view('index',compact('result','paginator'));