vendredi 18 septembre 2020

Laravel 5.8 query all entries which have all given related models

I've vehicles and vehicle_features. Those are combined via a many-to-many relationship. Let's say I have an array of IDs from vehicle_features. Now I want all vehicles which have all of the given features.

I've tried a combination of whereHas and whereIn, but it returns all vehicles with one match:

public function scopeTopFeatures(Builder $query, $topFeatureIds): Builder
{
    return $query->whereHas('features', function (Builder $query) use ($topFeatureIds) {
        $query->whereIn('vehicle_feature_id', $topFeatureIds);
    });
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire