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?
2 Answers 正确答案
Have you tried this?
Then, on your view, you can just loop through the apps.
If you want to set it as a relationship to category, you can use the
setRelation
method:Then in your view:
To get this to work, I had to bypass the Eloquent Relationships. I created a repository instead.
In this example, a user has lots of reports.
App\Repositories\ReportsRepository
This will get the reports records for a user.
ReportController
Here we call the ReportsRepositroy to get the records (I've removed the Auth code).
View - report/index.blade.php
The important bit for pagination here is {!! $reports->render() !!}. This generates the links of the pagination.
This is all that's needed. Laravel deals with the rest of the pagination magic itself.
Hope this helps.
来自 https://stackoverflow.com/questions/33507581/how-to-paginate-a-has-many-relationship-that-is-ordered