Reputation: 582
Post::with('comments.owner')->get();
This query will get us all the posts, its comments and comment owner .
But how can I get all the posts , its last 5 comments and authors for each comments . Can any one help please ?
Here is the schema Posts - id - content Comments - id - commented_id (user_id) - user_id User - id - full_name Thanks
Upvotes: 0
Views: 558
Reputation: 4435
Try this:
$posts = Post::with(['comments'=>function($query){
$query->orderBy('created_at','desc')->limit(5);
}])
->with('comments.author')
->get();
Upvotes: 1