Skip to content

Instantly share code, notes, and snippets.

@ewistrand
Last active February 17, 2020 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewistrand/d87868b642324f728718aa3c52dfb10a to your computer and use it in GitHub Desktop.
Save ewistrand/d87868b642324f728718aa3c52dfb10a to your computer and use it in GitHub Desktop.
Helper function to Paginate a Laravel Collections.
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
if(! function_exists('paginate'))
{
function paginate($items, $perPage = 15, $page = null)
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator(
$items->forPage($page, $perPage),
$items->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => 'page',
]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment