HUGE_DICK_10_INCHES's avatar

Returning pagination data as model collection

Is there a way to get pagination data inside collection?

return response()->
json(
    User::with(['posts' => function($q) {
        $q->paginate(5);
    }, 'posts.comments' => function($q) {
        $q->paginate(5, ['*'], 'commentsPage', request('commentsPage'));
    }])
    ->where('username', request('username'))
    ->firstOrFail()
);

I need to pass this as well: $q->nextPageUrl() for posts

0 likes
3 replies
jlrdw's avatar

I'm not understanding your question as results are in a collection anyway.

Snapey's avatar

no, you cannot paginate a relation

Please or to participate in this conversation.