先将数据库所查询的所有数据,放入数组 $list 中, 在利用数组函数将数据进行分页显示。关键代码如下:
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($list, ($current_page-1)*$perPage, $perPage); //利用数组函数进行分页查询
$total = count($list);//总数
$currentPage='';
$teacherAssistantList =new LengthAwarePaginator($item, $total, $perPage, $currentPage, [
'path' => Paginator::resolveCurrentPath(), //分页相关参数
'pageName' => 'page',
]);
// $list = $paginator->toArray()['data'];
return $this->success($list);