lundi 26 octobre 2015

Laravel Eagerloading Pivot Relation

The Model User has a ManyToMany relation to Work. The pivot also has a hasMany relation to the Model Location (one work can be done in different places from different User). I therefore use a custom pivot model UserWork. I try to load one Work with all associated User AND the Locations where the work was done:

Work::where('id', $id) // get the work with id
    ->with('users.pivot.locations') // method pivot not found
    ->with('users.user_work.locations') // method user_work not found

I dont understand why the pivot-part doesnt work because I would retrieve the locations like so: $work->users->pivot->locations

Any Idea on how to accomplish that? Maybe I have to use a closure in with() to load the needed pivot?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire