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
allmethod returns collection instead of query builder instance. You may usewithinstead ofloadand usepaginateto get the paginated result:Please note that according to Laravel docs: