Skip to content

Instantly share code, notes, and snippets.

@ctf0
Forked from vluzrmos/paginate.php
Last active October 20, 2021 01:05
Show Gist options
  • Save ctf0/109d2945a9c1e7f7f7ea765a7c638db7 to your computer and use it in GitHub Desktop.
Save ctf0/109d2945a9c1e7f7f7ea765a7c638db7 to your computer and use it in GitHub Desktop.
Laravel Paginate Collection or Array
<?php
// use Illuminate\Support\Collection;
// use Illuminate\Pagination\Paginator;
// use Illuminate\Pagination\LengthAwarePaginator;
/**
* Gera a paginação dos itens de um array ou collection.
*
* @param array|Collection $items
* @param int $perPage
* @param int $page
*
* @return LengthAwarePaginator
*/
public function paginate($items, $perPage = 15, $page = null, $getValues = true)
{
$pageName = 'page';
$page = $page ?: (Paginator::resolveCurrentPage($pageName) ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator(
$getValues
? $items->forPage($page, $perPage)->values()
: $items->forPage($page, $perPage),
$items->count(),
$perPage,
$page,
[
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
}
@ctf0
Copy link
Author

ctf0 commented Mar 8, 2021

anywhere u want ex controller

@mosesegboh
Copy link

anywhere u want ex controller

is the class already in laravel or i have to add the class?

@ctf0
Copy link
Author

ctf0 commented Mar 8, 2021

@shaho1090
Copy link

It works very well, Thanks dude....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment