#whereIn condition

5 messages · Page 1 of 1 (latest)

fervent minnow
#

Hi,

I have a many to many relation defined like this:

    public function facilities()
    {
        return $this->belongsToMany(
            UnitFacilityType::class,
            'unit_facilities',
            'unit_id',
            'unit_facility_type_id'
        );
    }

and inside the controller I make something like this:

      $items = $unitBuilder->whereHas('facilities', function (Builder $query) {
                $query->whereIn('unit_facility_types.id', [63, 64]);
            })->get();

            dd($items[3]);

$items returns me records either with id 63 or with id 64 and I want it to return only records that have both id 63 and id 64.

What is wrong?

Thanks!

barren steeple
#

What's wrong is that no facility type can have an id of both 63 and 64

#

For what you want to do you'd need two whereHas clauses, to check that you have a facility with id 63 and a facility with id 64

fervent minnow
#

and if I have more than two ids?

#

in reality I have more than 60 ids that I have to check