orm - Laravel eloquent: get data with model wherePivot equal to custom field -
i have eloquent object performer has albums , albums have images
here setup:
model performer->albums():
public function albums() { return $this->belongstomany('album','performer_albums','performer_id','album_id'); } model album->images()
public function images() { return $this->belongstomany('image','album_images','album_id','image_id')->withpivot(['type','size']); } i have performer object stored such:
$performer = performer::where...->first(); now need performer's albums images size 'large'
so avoid nesting queries, can use with()?
i tried
$performer->albums() ->with('images') ->wherepivot('size','large') ->get(); but laravel tells me it's trying use wherepivot performer-album relationship (m-2-m)
ps. aware can this,
$performer = performer::with('albums') ->with('albums.images') ->.....-conditions additional fields in album_images.... ->get(); but question remains same.
you need eager load constraints:
$performer->albums() ->with(['images' => function ($q) { $q->wherepivot('size','large'); }]) ->get(); and btw, no, can't this:
performer::with('albums') ->with('albums.images') ->.....-conditions additional fields in album_images.... ->get(); instead do:
performer::with(['albums.images' => function ($q) { $q-> .....-conditions additional fields in album_images.... }])->get();
Comments
Post a Comment