Answer the question
In order to leave comments, you need to log in
How to eagerly load a certain number of related models?
I have categories and news and use a many-to-many relationship. I need to make a request using eager loading to get all the categories so that each category has only three news items.
I'm trying to do this:
$categories = Category::with('news')->orderBy('id', 'ASC')->get();
$categories = Category::with(['news' => function($query)
{
$query->limit(3);
}])->orderBy('id', 'ASC')->get();
Answer the question
In order to leave comments, you need to log in
Apparently not:
Thehttps://laravel.com/docs/5.7/eloquent-relationship...limit
andtake
query builder methods may not be used when constraining eager loads.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question