For the sake of example, say I have the following, where there is a relationship set up for each person having one hat.
$people = People::join('hat')->get();
How do I filter my results to only give me the people whose hats are red?
I have tried,
$people = People::join('hat')
->where('hat.colour', 'red')
->get();
...but no luck so far.
Cheers
Use whereHas():
$people = People::whereHas('hat', function($query) {
$query->where('colour', 'red');
})->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With