i am trying to customize laravel pagination to fix this issue:
{
"current_page": 2,
"data": {
"10": {},
"11": {},
}
i want the result to be like this:
{
"current_page": 1,
"data": [
{},
{},
}
when i use the whole collection it works good, but if get part of the collection using (where), this issue appear for me.
this coming script of code has been used in the AppServiceProvider boot method:
Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
i found the solution for my issue by replacing this code:
Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
with this one:
if (!Collection::hasMacro('paginate')) {
Collection::macro('paginate', function ($perPage = 15, $page = null, $options = []) {
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
return (new LengthAwarePaginator( $this->forPage($page, $perPage)->values()->all(), $this->count(), $perPage, $page, $options))->withPath('');
});
}
AppServiceProvider boot method.