2 years ago

Nested eager loading

Posted 2 years ago by UAVova

I want to eager load post comments and it's owner. At the same time, i want to order comments by created_at column.

To order not nested relation, i use:

$post = Post::with(['comments' => function ($query) { $query->orderBy('created_at', 'desc'); }])->find($id);

So, how can i do something like:

$post = Post::with(['comments.owner' => function ($query) { $query->orderBy('comments.created_at', 'desc'); }])->find($id);

Please sign in or create an account to participate in this conversation.