I would investigate using a "local scope" on the model because it seems you will be using that query more than once. Something like:
public function scopeFeatures($query)
{
return $query->where('features', 1);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I'm pretty lost with laravel functions. Haven't found any good materail to learn from. If anyone can share a good place to learn, give me link please.
So problem right now: 3 tables - 'breeds', 'features', and 'breed_feature' User can fill a form with checkboxes and select many features. No I want to get only breeds that have all these features set.
I have all model set up and everything else is working. I can add features to breeds and show all features breed has, but how do I get only breeds that have selected features.
Thanks
so, you want to get all breeds that have all selected features ?
try iterate the feature ids to build your query, like
foreach ($ids as $id) {
$query->where('feature_id', $id);
}
you can append it inside your whereHas query.
Please or to participate in this conversation.