Laravel 固定每页显示条数和显示页数(超过部分直接返回空数据)

Laravel框架
442
0
0
2022-04-13
标签   Laravel基础
use Illuminate\Pagination\LengthAwarePaginator;

$paginate = $model->paginate(25, ['*'], 'page', $page);
$paginate->setCollection($paginate->getCollection()->map(function (Customer $customer) {
    $data = $customer->toArray();
    $data['mobile'] = getHiddenMobile($data['mobile']);
    return $data;
}));

// 每页显示数量固定25条
// 强制最多只能显示5页,共125条数据
// 超过部分直接返回空数据
$new = new LengthAwarePaginator(
    $page > 5 ? [] : $paginate->items(),
    min($paginate->total(), 125),
    $paginate->perPage(),
    $paginate->currentPage(),
    $paginate->getOptions()
);