Created
July 20, 2016 14:31
-
-
Save vluzrmos/3ce756322702331fdf2bf414fea27bcb to your computer and use it in GitHub Desktop.
Laravel Paginate Collection or Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Gera a paginação dos itens de um array ou collection. | |
* | |
* @param array|Collection $items | |
* @param int $perPage | |
* @param int $page | |
* @param array $options | |
* | |
* @return LengthAwarePaginator | |
*/ | |
public function paginate($items, $perPage = 15, $page = null, $options = []) | |
{ | |
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1); | |
$items = $items instanceof Collection ? $items : Collection::make($items); | |
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options); | |
} |
Hey, isn't it solved yet.
…On Wed, Feb 16, 2022 at 6:10 AM vladimir-zarcanin ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
If you have a problem with pagination add values() method:
return new LengthAwarePaginator($items->forPage($page,
$perPage)->values(), $count, $perPage, $page, []);
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/3ce756322702331fdf2bf414fea27bcb#gistcomment-4066762>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AM22RZIVTDGP2G6H4WZWNLLU3LTOPANCNFSM4HYWPWPA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you commented.Message ID:
***@***.***>
I have a problems when fetch page > 1, $items->forPage($page, $perPage)->values()
. this solved my problems.
This type of pagination can impact performance?
Thanks very much
Laravel 9.x and above
public static function paginate(array|Collection $items, mixed $perPage = null, mixed $page = null, array $options = [], mixed $path = null)
{
$perPage = is_null($perPage) ? 25 : $perPage;
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
$paginator = new LengthAwarePaginator(
$items->forPage($page, $perPage),
$items->count(),
$perPage,
$page,
$options
);
if(!is_null($path)) {
$paginator->setPath($path);
}
return $paginator;
}
Basic Example:
// set path dynamically from current route name or url with params
$path = route(request()->route()->getName(), $request->except(['page']));
$invoices = YOUR_HELPER_CLASS_HERE::paginate(
items: $data,
perPage: 10,
path: $path
);
Laravel 10.x / PHP 8.2 and above:
public static function paginate(
array|Collection $items,
?int $perPage = null,
?int $page = null,
array $options = [],
?string $path = null
): LengthAwarePaginator {
$perPage ??= 50;
$page ??= Paginator::resolveCurrentPage(default: 1);
$path ??= Paginator::resolveCurrentPath();
$items = $items instanceof Collection ? $items : Collection::make($items);
$paginator = new LengthAwarePaginator(
$items->forPage($page, $perPage),
$items->count(),
$perPage,
$page,
$options
);
if (!blank($path)) {
$paginator = $paginator->setPath($path);
}
return $paginator;
}
Usage:
$paginated_list = Helper_Class::paginate($list_of_items);
Collection::macro('paginate', function($perPage = 15, $page = null, $options = []) {
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
if (!isset($options['path'])) {
$options['path'] = '/' . request()->path();
}
return new LengthAwarePaginator(array_values($this->forPage($page, $perPage)->toArray()), $this->count(), $perPage, $page, $options);
});
Failed in L10
Place this in the boot method of your App\Providers\AppServiceProvider;
Collection::macro('paginate', function ($perPage = 15, $page = null, $options = []) {
$page = $page ?: (\Illuminate\Pagination\Paginator::resolveCurrentPage() ?: 1);
return new \Illuminate\Pagination\LengthAwarePaginator(array_values($this->forPage($page, $perPage)->toArray()), $this->count(), $perPage, $page, $options);
});
Use like this
$perPage = 5;
collect([1,2,3,4,5])->paginate($perPage)
Great, thanks! 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you have a problem with pagination add values() method:
return new LengthAwarePaginator($items->forPage($page, $perPage)->values(), $count, $perPage, $page, []);