Answer the question
In order to leave comments, you need to log in
How to get posts with comments in Laravel 4?
You need to get records with comments by type like VK. That is, the cycle displays posts and 3 last comments to them. I do this:
In the Posts model:
public function lastcomments()
{
return $this->hasMany('Comment')->orderBy('created_at','DESC')->limit(3);
}
$arr = array(1,2,3 ...)
Post::whereIn('id',$arr)->orderBy('id', 'desc')->with("lastcomments")->get();
Answer the question
In order to leave comments, you need to log in
I was wrong, I apologize.
This is a fundamental limitation for eager loading, and the current behavior is the only thing it can provide. That is, there is no point in using hasMany here.
That is, comments must be received in separate requests, according to the number of posts, which brings us back to the previous denormalization task. Although I would not be in a hurry to do it, since 20 queries on the index will not load the database.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question