AtomCoder's avatar

Pagination issue when using diff() function

Hi Guys,

I've just came across an issue with pagination when using the diff() function. The result actually paginates the data as expected, but I'm unable to use the $data->links() function within my blade file.

I'm essentially trying to get the difference of two collections and the paginate the result, heres what I'm using to do that (simplified version without any where clauses for clarity)

$league = League::find($request->id);

$data = $league->competitors()->paginate(10)->diff( $league->marshals()->get() );

return view('league.competitors')->with(compact('data'));

Any ideas why links() function does not exist in blade ?

0 likes
4 replies
bobbybouwmann's avatar

It's because the diff returns a new collection and not the pagination object anymore.

If you want to run a diff before you paginate, you need to grab the collection, do the diff and then paginate the remaining results.

An even better approach would be doing the diff part in the query. Something like this

$data = $league->competitors()
    ->whereNotIn('id', $league->marshals()->get->pluck('id'))
    ->paginate(10);
AtomCoder's avatar

Looking at your first solution, if I do this:

$data = $league->competitors()->get()->diff( $league->marshals()->get() );

$data is now a new collection, so what you're saying is that I now need to paginate $data ?

AtomCoder's avatar

Sorry @bobbybouwmann I'm only getting back to this now.

I'll play about with those possible solutions and mark this thread as necessary. Thanks again for your help.

Please or to participate in this conversation.