有时我们可能想要通过传递数组数据来手动创建分页实例,代码如下:
use Illuminate\Pagination\LengthAwarePaginator;
public function getPaginatedArray($array, $perPage, $page)
{
$offset = ($page - 1) * $perPage;
return new LengthAwarePaginator(
array_slice($array, $offset, $perPage, true),
count($array),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => 'page',
]
);
}
$array = [...];
$perPage = 10;
$page = request()->get('page', 1);
$paginatedArray = $this->getPaginatedArray($array, $perPage, $page);
dd($paginatedArray);