Laravel 5.1在雄辩的with()方法中使用限制 [英] Laravel 5.1 use limit in with() method of eloquent

查看:77
本文介绍了Laravel 5.1在雄辩的with()方法中使用限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$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()方法中使用限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆