I would like to know how I can paginate the called data of a relationship in Laravel, I do not want to load all the information in the same route, couse it would make the load heavier
I have categories and products, in the resource categories, I am calling the products through a relationship, but what I need to do is paginate those products
I need that in the route api/category/1, it returns the name of the category and the products on that specific category, (witch is working) but need those products paginated.
In my category controller:
public function show($id)
{
$category = Category::findOrFail($id);
return new CategoryResource($category);
}
In my category model I have:
public function products()
{
return $this->hasMany('App\Product');
}
In my category resource I have:
return [
'name' => $this->name,
//staff
'products' => $this->products,
];
What I need to do is paginate that 'products' => $this->products,
I already try 'products' => $this->products->paginate(5),
but of course dont works, also try return $this->hasMany('App\Product'->paginate(5));
in my model but it also doesn't work.
Thank you in advance :)