Laravel 5.1 在 eloquent 的 with() 方法中使用限制

Laravel 5.1 use limit in with() method of eloquent

Eloquent

$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

limit

例如

有 10 个 staff_group 然后它给出了 5 个 staffgrouprights 的记录 10 staff_group 但我想要它 5 个 staff_group

这里用staffGroupRightreturndata适合idstaff group.

但我想在 with() 方法数据中设置 limit

是否可以在with()方法中设置limit...??

$staffGroup = StaffGroup::where('id', $id)
        ->with(['staffGroupRight' => function($query){
            return $query->take(10);
            }])
        ->first();

我假设你想获取 staffGroupRight 的 10 条记录。