I am trying to paginate a Eloquent relationship like this:
$query = Product::find(1)->options()->paginate();
But I get the following
Create a custom length-aware paginator.
$options = new LengthAwarePaginator(Product::find(1)->options()->
->skip(($request->input('page', 1) - 1) * 10)->take(10)->get(),
Product::find(1)->options()->count(),
10, $request->input('page', 1),
[
// paginator options here
]);
$query = Product::find(1)->get()->options()->paginate();
Try adding get
You can not lazy load relational pagination like that, instead in your Product Model put the following function below your options has many relationship
public function getOptionsPaginatedAttribute()
{
return $this->options()->paginate(10);
}
This will allow you to call the pagination on your relational data by
$product->options_paginated