如何对“有很多"进行分页关系是有序的吗? [英] How to paginate a "has many" relationship that is ordered?
本文介绍了如何对“有很多"进行分页关系是有序的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对以下关系(具有许多应用程序的类别)进行分页:
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');
}
}
问题是,当我在该行的末尾添加->paginate(10);
时,出现以下错误:
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
我在这里想念什么?
推荐答案
您尝试过吗?
$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() !!}
如果要将其设置为与类别的关系,则可以使用setRelation
方法:
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');
然后在您看来:
@foreach ($category->apps as $app)
{{ $app->id }}
@endforeach
{!! $category->apps->render() !!}
这篇关于如何对“有很多"进行分页关系是有序的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文