Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eltso's avatar
Level 1

Many to many query

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

0 likes
6 replies
ejdelmonico's avatar

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);
    }
eltso's avatar
Level 1

Sry but not sure how to apply it to my case. What I have done so far:

$featureids = request('features');
        $breeds = Breed::whereHas('features', function($query) use ($featureids) {
            $query->where('breed_feature_id', $featureids);
        })->get();
        dd($breeds);

This kinda works. $featureids gets array of feature id-s from form. Now if I have breeds that have these features, then everything seems to work. Now if I select features that I know that no breeds will match, I still get one random breed from this query.

bunnypro's avatar
bunnypro
Best Answer
Level 7

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.

1 like
eltso's avatar
Level 1

Thanks, this does the job :)

eltso's avatar
Level 1

Damn, still nogo, now if I select more than 1 feature, I always get empty collection, so it works if i choose one feature, but more than one is empty result.

eltso's avatar
Level 1

Ok, tried same thing with query builder now:

 $featureids = request('features');
        $breeds = DB::table('breeds')
            ->join('breed_breed_feature', 'breeds.id', '=', 'breed_breed_feature.breed_id')
            ->join('breed_features', 'breed_features.id', '=', 'breed_breed_feature.breed_feature_id')
            ->select('breeds.*')
            ->where(function($q) use ($featureids) {
                foreach($featureids as $id)
                {
                    $q->where('breed_breed_feature.breed_feature_id', $id);
                }
            })
            ->get();
        dd($breeds);

Still same problem, if I select 1 id from features, I get all breeds that have this one feature, but if I select 2 or more, then I get empty collection. I've read all the google already and tried every guide I have found but I just can't get this thing to work.

Please or to participate in this conversation.