Laravel Paginate Collection or Array
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 | |
use Illuminate\Support\Collection; | |
use Illuminate\Pagination\Paginator; | |
use Illuminate\Pagination\LengthAwarePaginator; | |
/** | |
* Gera a paginação dos itens de um array ou collection. | |
* | |
* @param array|Collection $items | |
* @param int $perPage | |
* @param int $page | |
* | |
* @return LengthAwarePaginator | |
*/ | |
public function paginate($items, $perPage = 15, $page = null, $getValues = true) | |
{ | |
$pageName = 'page'; | |
$page = $page ?: (Paginator::resolveCurrentPage($pageName) ?: 1); | |
$items = $items instanceof Collection ? $items : Collection::make($items); | |
return new LengthAwarePaginator( | |
$getValues | |
? $items->forPage($page, $perPage)->values() | |
: $items->forPage($page, $perPage), | |
$items->count(), | |
$perPage, | |
$page, | |
[ | |
'path' => Paginator::resolveCurrentPath(), | |
'pageName' => $pageName, | |
] | |
); | |
} |
u can use any of the extra methods
$this->paginate($data)->withPath('?type=all'); $this->paginate($data)->appends(['type'=>'all']); $this->paginate($data)->fragment('all');
Great...thank you
how can you install it please??
anywhere u want ex controller
anywhere u want ex
controller
is the class already in laravel or i have to add the class?
It works very well, Thanks dude....
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
u can use any of the extra methods