Skip to content

Instantly share code, notes, and snippets.

@ahkmunna
Last active August 6, 2018 22:23
Show Gist options
  • Save ahkmunna/210e5cd19c72942b8520f65a879447bd to your computer and use it in GitHub Desktop.
Save ahkmunna/210e5cd19c72942b8520f65a879447bd to your computer and use it in GitHub Desktop.
Add pagination functionality on Laravel collections. Add this macro in the boot method in AppServiceProvider
// Add pagination on collections
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), $this->count(), $perPage, $page, $options))
->withPath(url()->full());
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment