Skip to content

Instantly share code, notes, and snippets.

@gausoft
Forked from ctf0/paginate.php
Last active January 10, 2020 13:08
Show Gist options
  • Save gausoft/c673817f1343eeb31812d2b1c7544a50 to your computer and use it in GitHub Desktop.
Save gausoft/c673817f1343eeb31812d2b1c7544a50 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;
/**
* Génère la pagination des éléments dans un tableau ou une collection.
*
* @param array|Collection $items
* @param int $perPage
* @param int $page
*
* @return LengthAwarePaginator
*/
public function paginate($items, $perPage = 15, $page = null)
{
$pageName = 'page';
$page = $page ?: (Paginator::resolveCurrentPage($pageName) ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator(
$items->forPage($page, $perPage)->values(),
$items->count(),
$perPage,
$page,
[
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment