I have colors request in array $colors = collect($request->colors);
i am trying when query builder for make request when its not empty
$products = Product::when($colors, function ($query, $colors) {
return $query->whereHas('colors', function (Builder $query) use ($colors) {
$query->whereIn('slug', $colors->toArray());
});
})
it should not be trigger color query
when i try
when(!$colors->isEmpty(), function ($query, $colors)
its trigger when there is array data in colors request
but inside query its return boolean instead of original array data
help!
trueorfalse, so if you do!$colors->isEmpty()that transforms automatically into abooleanand that is what$colorswill be, so you have to use first code, not second. So, what is your problem with the first one ?true$colors->isNotEmpty() ? $colors : false.