I would like to paginate the following relationship (a Category having many Apps):
class Category extends Model
{
public function apps()
{
return $this->hasMany('App\App')->orderBy('current_price', 'asc');
}
}
The problem is, when I add ->paginate(10);
to the end of that line, I get the following error:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
What am I missing here?
Have you tried this?
$category = Category::first();
$apps = $category->apps()->paginate(10);
return view('example', compact('category', 'apps'));
Then, on your view, you can just loop through the apps.
@foreach ($apps as $app)
{{ $app->id }}
@endforeach
{!! $apps->render() !!}
If you want to set it as a relationship to category, you can use the setRelation
method:
$category = Category::first();
$category->setRelation('apps', $category->apps()->paginate(10));
return view('example', compact('category');
Then in your view:
@foreach ($category->apps as $app)
{{ $app->id }}
@endforeach
{!! $category->apps->render() !!}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments