Created
May 21, 2020 16:50
-
-
Save pravodev/00791415e1fa727f19ba28dd4a772278 to your computer and use it in GitHub Desktop.
Laravel Pagination on Collection
This file contains 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
<?php | |
namespace App\Providers; | |
use Illuminate\Support\Collection; | |
use Illuminate\Pagination\LengthAwarePaginator; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
public function boot() | |
{ | |
Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') { | |
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName); | |
return new LengthAwarePaginator( | |
$this->forPage($page, $perPage), | |
$total ?: $this->count(), | |
$perPage, | |
$page, | |
[ | |
'path' => LengthAwarePaginator::resolveCurrentPath(), | |
'pageName' => $pageName, | |
] | |
); | |
}); | |
} | |
} |
This file contains 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
// in controller just use paginate like usually | |
// example | |
collect([1,2,3,4,5])->paginate(10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment