6

Eloquent

$staffGroup = StaffGroup::where('id', $id)
            ->with('staffGroupRight')
            ->first();

In StaffGroup Model:

public function staffGroupRight() {
    return $this->hasMany('Modules\Staff\Http\Models\StaffGroupRight');
}

what i have does is,

public function staffGroupRight() {
    return $this->hasMany('Modules\Staff\Http\Models\StaffGroupRight')->take(5);
}

but it gives total 5 rows for all staff_group but i want it to limit for one staff_group

For example

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

here with staffGroupRight return data appropriate to id of staff group.

but i want to set limit in that with() method data.

is it possible to set limit in with() method or not...??

Sagar Naliyapara
  • 3,971
  • 5
  • 41
  • 61

1 Answers1

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

I assume you want to take 10 record of staffGroupRight.

  • 2
    Thank you but it take(10) as all over but i want it as per staffgroupright...what i mean is there is multiple staffgroup and then for total staffgroup it return 10 but i want it as per staffgroup it means for each staffgroup i want 10 staffgroupright – Sagar Naliyapara Dec 04 '15 at 06:47
  • I tested it out, it seems like no easy way to do this. except for looping each staffgroup like `foreach($staffGroup as $s){ $s->load(['staffGroupRight' => function($q){ return $q->take(5);]}); }` – Yap Kai Lun Leon Dec 04 '15 at 07:40
  • 1
    @SagarNaliyapara why was this answer marked accepted? It clearly did not answer your question, as you explained in your comment. – miken32 Dec 07 '21 at 17:29