You can't do it with eager loading. Moreover it's pretty hard to do in SQL as you would need many subqueries to limit joined rows. So instead load everything and limit comments in php or lazy eager load recent comments.
To do the latter you can use something like this on your Post model to make things easier:
public function commentsRecent()
{
return $this->hasMany('Comment')->orderBy('created_at', 'desc')->limit(3);
}
moh1434 liked this reply
I have found the solution. Try this!
$lines = \Category::where('status', \Category::STATUS_PUBLISH)
->with(['firstThreeComment'])
->get()
->map(function( $category ){
$category->firstThreeComment = $category->firstThreeComment->take(3);
return $category;
});
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community