Reputation: 1
I asked to create a page that displays a list of all the categories and the last 3 products for each category.
There is a relationship between the category and the products.
I am trying to generate a route that will return a Json response.
Here is my code:
$category = Category::all()->load(["products"=>function($res){
$res->take(3);
}]);
return $category->paginate(2);
But It returns an error:Collection::paginate does not exist.
What does it mean?
Thank you
Upvotes: 0
Views: 552
Reputation: 9029
all
method returns collection instead of query builder instance. You may use with
instead of load
and use paginate
to get the paginated result:
return Category::with(["products" => function($res) {
$res->take(3);
}])->paginate(2);
Please note that according to Laravel docs:
The
limit
andtake
query builder methods may not be used when constraining eager loads.
Upvotes: 1