Laravel Eloquent Many-to-Many query whereIn
Try
$associateDogs = Dog::with(['owners' => function($query) use ($listOfOwners) {
$query->whereIn('id', $listOfOwners);
}])->get();
I tried the two suggested solutions but I was getting an error that said that id was not unique. I solved like this:
$dogs = Dog::whereHas('owners', function($q) use($ownerIds) {
$q->whereIn('owner_id', $ownerIds);
})->get();
Use the whereHas()
method:
$dogs = Dog::whereHas('owners', function($q) use($ownerIds) {
$q->whereIn('id', $ownerIds);
})->get();