Skip to content

Instantly share code, notes, and snippets.

@anaxamaxan
Created November 26, 2024 05:39
Show Gist options
  • Save anaxamaxan/b06c2ee4e5e2c2a400b3b569b3b926be to your computer and use it in GitHub Desktop.
Save anaxamaxan/b06c2ee4e5e2c2a400b3b569b3b926be to your computer and use it in GitHub Desktop.
// app/Providers/MacroServiceProvider
public function boot(): void
{
Collection::macro('paginate', $this->paginate());
}
private function paginate(): \Closure
{
/**
* Paginate a standard Laravel Collection.
*/
return function(int $perPage = 15, ?int $total = null, ?int $page = null, string $pageName = 'page'): LengthAwarePaginator
{
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment