Friday, 15 April 2011

php - Laravel: Combine dynamic scope and with -



php - Laravel: Combine dynamic scope and with -

i trying eager load dynamic scope in laravel 4.2

i have next eloquent class:

class project extends eloquent { protected $table = 'projects'; public function subprojects() { homecoming $this->hasmany('subproject'); } }

on subproject class, have next dynamic scope:

public function scopeforuserinperiod($query, $user, $interval) { homecoming $query->wherehas('hourregistrations', function($query) use($user, $interval) { $query->where('user_id', $user->id)->wherebetween('date', [$interval->from, $interval->to]); }); }

now eagerload scope when load projects, like.

project::with('subprojects.foruserinperiod', $user, $period);

unfortunately, with accepts list of relations load. can't seem find indications whatsoever on how supposed eagerload scopes take arguments. :-(

you don't pass else relations when eager/lazy loading.

you utilize dots indicate nested relations, not methods on query.

this want:

project::with(['subprojects' => function ($q) utilize ($user, $period) { $q->foruserinperiod($user, $period); }])->get();

php laravel eloquent eager-loading

No comments:

Post a Comment