How can I paginate a merged collection in Laravel 5?
however paginate is for eloquent models and DB queries, and not collections, it seems.
You are right. but there is ineed a paginator function for collections. forPage
Syntax:
Collection forPage(int $page, int $perPage)
Example:
Rest is simple.
public function foo()
{
$collection = collect([1,2,3,4,5,6,7,8,9,0]);
$items = $collection->forPage($_GET['page'], 5); //Filter the page var
dd($items);
}
If you want to use a LengthAwarePaginator simply instantiate one. As mentioned in the comments of a previous answer you will have to set the path for this. You will also need to make sure you resolve the "currentPage" and set the items to be returned before you instantiate the paginator. This can all be done before/on instantiation. So a function may look something like:
function paginateCollection($collection, $perPage, $pageName = 'page', $fragment = null)
{
$currentPage = \Illuminate\Pagination\LengthAwarePaginator::resolveCurrentPage($pageName);
$currentPageItems = $collection->slice(($currentPage - 1) * $perPage, $perPage);
parse_str(request()->getQueryString(), $query);
unset($query[$pageName]);
$paginator = new \Illuminate\Pagination\LengthAwarePaginator(
$currentPageItems,
$collection->count(),
$perPage,
$currentPage,
[
'pageName' => $pageName,
'path' => \Illuminate\Pagination\LengthAwarePaginator::resolveCurrentPath(),
'query' => $query,
'fragment' => $fragment
]
);
return $paginator;
}
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Pagination\Paginator;
You can add the following code for Collection in the Providers/AppServiceProvider inside public function boot() .
// Enable pagination
if (!Collection::hasMacro('paginate')) {
Collection::macro('paginate',
function ($perPage = 15, $page = null, $options = []) {
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
return (new LengthAwarePaginator(
$this->forPage($page, $perPage)->values()->all(), $this->count(), $perPage, $page, $options))
->withPath('');
});
}
Then, you can call paginate from a Collection, just like an Eloquent model. For example
$pages = collect([1, 2, 3, 4, 5, 6, 7, 8, 9])->paginate(5);
You might try paginating both sets and merging them. You can find more information about pagination in the docs and the api. Here is an example of manually creating your own paginator...
$perPage = 20;
$blue = BluePerson::paginate($perPage / 2);
$red = RedPerson::paginate($perPage - count($blue));
$people = PaginationMerger::merge($blue, $red);
I have included the PaginationMerger class below.
use Illuminate\Pagination\LengthAwarePaginator;
class PaginationMerger
{
/**
* Merges two pagination instances
*
* @param Illuminate\Pagination\LengthAwarePaginator $collection1
* @param Illuminate\Pagination\LengthAwarePaginator $collection2
* @return Illuminate\Pagination\LengthAwarePaginator
*/
static public function merge(LengthAwarePaginator $collection1, LengthAwarePaginator $collection2)
{
$total = $collection1->total() + $collection2->total();
$perPage = $collection1->perPage() + $collection2->perPage();
$items = array_merge($collection1->items(), $collection2->items());
$paginator = new LengthAwarePaginator($items, $total, $perPage);
return $paginator;
}
}
best way for paginate collection:
1- add this to boot function in \app\Providers\AppServiceProvider
/*
* use Illuminate\Support\Collection;
* use Illuminate\Pagination\LengthAwarePaginator;
*
* 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),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
2-From hereafter for all collection you can paginate like your code
$people->paginate(5)