Created
July 26, 2019 22:23
-
-
Save freelance-github/fe8488ff19b1a7ed67223b15c7a25b52 to your computer and use it in GitHub Desktop.
laravel collection pagination
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
<?php | |
namespace App\Providers; | |
use Illuminate\Pagination\LengthAwarePaginator; | |
use Illuminate\Support\Collection; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
/** | |
* Paginate a standard Laravel Collection. | |
* | |
* @param int $perPage | |
* @param int $total | |
* @param int $page | |
* @param string $pageName | |
* @return array | |
*/ | |
Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') { | |
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName); | |
return new LengthAwarePaginator( | |
$this->forPage($page, $perPage)->values(), | |
$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