pkracer
pkracer

Reputation: 173

Laravel 4.1 - Paginating nested eager load issues

Is it possible to paginate the parent of a nested eager load?

My three models are:

Event -has many participants

Participant - has many products

Product

Currently, my code looks like...

return Event::with(array('participants.products' => function ($q) {
$q->paginate(100); }))->where('id', $id)->get();

This code does work as it returns my event, the event participants, and those participant's products. The issue I am having is that the paginating seems to be applied to only products. I would like it to be applied to only participants instead. The goal would be to return my event, 100 participants, and then all products for those 100 participants.

Is this possible?

Upvotes: 1

Views: 288

Answers (1)

user1669496
user1669496

Reputation: 33078

I believe something like this should work for you.

return Event::with(array('participants' => function($q)
{
    $q->paginate(100);
}, 'participants.products'))->where('id', $id)->get();

Upvotes: 4

Related Questions