Search code examples
laravel-5paginator

Manual paginator on merged collection


I have a merged collection I want to paginate and I can't seem to figure this out.

First off, the reason I need to create the paginator manually is because I fetch 2 collections first and merge them, like so:

$a = ModelA::all();
$b = ModelB::all();

$c = $a->merge($b);

Next step is to paginate this collection, I tried the Paginator and the LengthAwarePaginator

PAGINATOR

$page = 1;
$results = new Paginator($c, 2, $page);

Here I get the 2 first results in a paginator

$page = 2;
$results = new Paginator($c, 2, $page);

I still get the 2 first results, while i'd expect the third and fourth result (the collection is more then 2 elements long!)

LENGTHAWAREPAGINATOR

$page = 1;
$results = new LengthAwarePaginator($c, count($c), 2, $page);

Here I get a Paginator but the items contain all elements of the collection, no matter what page number i ask for (instead of the 2 i'm asking for)

Any ideas on what might be the problem? Thank you in advance


Solution

  • According to the Laravel community this is intentional. You should slice your items before passing it to the Paginator.