I want to filter the data with array and return the data with every elements is exists
not containing
.
For example I have cars
with colors ['black', 'white', 'red']
.
My focus is when I filter with this array ['black', 'white', 'yellow']
it should return empty cars
I already try with whereIn
but in the docs says it return with containing
array values.
//available colors are black, white, red
$filter = ['black', 'white', 'yellow']; //bcs there is 'yellow' in filters I want the query gives empty result
$cars = Car::whereHas('colors', function($q) use ($filter){
$q->whereIn('color_name', $filter);
})->get();
//still return the cars
Thank you for any help.
CodePudding user response:
It sounds like you want a where
condition for each selected colour:
$cars = Car::whereHas('colors', function($q) use ($filter) {
foreach ($filter as $color) {
$q->where('color_name', $color);
}
})->get();
CodePudding user response:
Laravel has the function whereIn
Documentation here
$filter = ['black', 'white', 'yellow'];
$cars = Car::whereIn('color_name', $filter)->get();