I have a Post model that has hasMany ('Comments'). I would like to receive all posts with comments, but only the last comment for each post. And since there are thousands of posts with thousands of comments each, this option is not possible due to performance problems (that is, downloading all the comments for each post, and then executing $ post-> comments [0] โ value):
Post::with('comments' => function($query){ $query->orderBy('created_at','desc') });
I can not, too:
Post::with('comments' => function($query){ $query->orderBy('created_at','desc')->limit(1) });
since it just doesn't work.
I am absolutely sure that I am not the only one who has this problem, and I managed to find some โsolution attemptsโ, but not a stable example of working code. Can anyone help?
source share