BrianDillingham's avatar

Method render does not exist. Eager / Nested Pagination

I am only getting the amount I set in paginate() ie 4 snippets & 15 likes but in the view I call $user->snippets->render() and get Method render does not exist Any help would be much appreciated

$user->load([
    'snippets' => function ($query) { $query->paginate(4); },
    'likes'    => function ($query) { $query->paginate(15); },
]);

dd($user->snippets->render());

0 likes
6 replies
Snapey's avatar

I doubt that you can have pagination on a relation?

BrianDillingham's avatar

@Snapey Thats what Im coming to think as well, but it does reduce the relation results, just not providing the pagination methods

Snapey's avatar

You could get the user and then one of the relations (as a separate collection), then paginate on that one? Not sure from a UI perspective how you could paginate two different sets at the same time?

Snapey's avatar

The other option is to show the user, then have javascript load the two relations as paginated collections so that you can have separate paginate controls next to each collection?

wiedem's avatar

@BrianDillingham I don't quite understand what you're trying to do.

If you just want to paginate through a to-many relation on a single model instance you can do so by directly calling paginate on the relations

$snippets = $user->snippets()->paginate(4);
$likes = $user->likes()->paginate(15);

(note that you would actually have to use different page names if you really want to use multiple paginators in one view)

The load method is meant to be used for eager loading but your example doesn't need eager loading. I.e. your query doesn't have the N+1 problem.

Cached relation values ($relations property) are always the result of the getResults method of a relation object. Which means it's either a single model object or a collection but never a paginator. Calling paginate on a query or relation will return a Paginatorinstance on the other hand.
Your code fails because you're trying to call render on a collection instance.

1 like

Please or to participate in this conversation.