Created
November 26, 2024 05:39
-
-
Save anaxamaxan/b06c2ee4e5e2c2a400b3b569b3b926be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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