0
votes

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

1
please post your DB schemajaysingkar
Posts - id - content Comments - id - commented_id (user_id) - user_id User - id - full_namemaj

1 Answers

1
votes

Try this:

$posts = Post::with(['comments'=>function($query){
                $query->orderBy('created_at','desc')->limit(5);
            }])
            ->with('comments.author')
            ->get();