问题描述
$staffGroup = StaffGroup::where('id', $id)
->with('staffGroupRight')
->first();
在StaffGroup
Model
中:
public function staffGroupRight() {
return $this->hasMany('Modules\Staff\Http\Models\StaffGroupRight');
}
我要做的是
public function staffGroupRight() {
return $this->hasMany('Modules\Staff\Http\Models\StaffGroupRight')->take(5);
}
但它为所有staff_group
总共提供了5行,但我希望它为一个staff_group
but it gives total 5 rows for all staff_group
but i want it to limit
for one staff_group
有10个staff_group
,那么该10个staff_group
给出5个staffgrouprights
记录,但是我希望它为单个staff_group
There are 10 staff_group
then it gives 5 records of staffgrouprights
for that 10 staff_group
but i want it 5 for single staff_group
此处带有staffGroupRight
,返回与staff group
中的id
相称的data
.
here with staffGroupRight
return data
appropriate to id
of staff group
.
但是我想在该with()
方法数据中设置limit
.
but i want to set limit
in that with()
method data.
是否可以在with()
方法中设置limit
......
is it possible to set limit
in with()
method or not...??
推荐答案
$staffGroup = StaffGroup::where('id', $id)
->with(['staffGroupRight' => function($query){
return $query->take(10);
}])
->first();
我假设您要记录10个staffGroupRight记录.
I assume you want to take 10 record of staffGroupRight.
这篇关于Laravel 5.1在雄辩的with()方法中使用限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!