Answer the question
In order to leave comments, you need to log in
Laravel limit for with?
Category::with(['products' => function($query) {
$query->take(3);
}])->where('type', 1)->get();
Answer the question
In order to leave comments, you need to log in
You cannot do this in Eloquent. In fact, you only get 3 categories. Therefore, if there are not many products, you can select categories, and then make a map through the collection and remove unnecessary products. Or you can do something like:
$categories = Category::where('type', 1)->get();
$categories->each(function($category) {
$category->products()->take(3)->get();
});
This package helped
https://github.com/staudenmeir/eloquent-eager-limit
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question