laravel 数组手动分页

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator; 
$perPage = 10;            //每页显示数量
if ($request->has('page')) {
    $current_page = $request->input('page');
    $current_page = $current_page <= 0 ? 1 :$current_page;
} else {
    $current_page = 1;
}
$item = array_slice($Array, ($current_page-1)*$perPage, $perPage);//$Array为要分页的数组
$totals = count($Array);
$paginator =new LengthAwarePaginator($item, $totals, $perPage, $current_page, [
    'path' => Paginator::resolveCurrentPath(),
    'pageName' => 'page',
]);

return response()->json(['code'=> 200,'msg'=>'ok','data'=>$paginator]);

  

转自:https://blog.csdn.net/qq_39191303/article/details/80564135

posted @ 2021-08-18 14:45  dreamboycx  阅读(96)  评论(0编辑  收藏  举报