Not tested but shouldn't it be enough to use:
function ($query) use ($request) {
$query->whereIn('envs', $request->get('env'));
}
So I search my model for a column which I have defined the cast as array. It's a lists of ID's and I got a list to match it against, if it got a ID which is in my list I want it.
**code**->when($request->has('env'), function ($query) use ($request)
{
return $query->where(function ($query) use ($request)
{
foreach($request->env as $env)
{
$query->orWhereJsonContains('envs', $env);
}
});
})->**more_code**
This works, but I want to know if there is a better way as I personally don't like this approach EDIT: I forgot that I could use orWhereJsonContains
@joveice Ah ok, I missed that. So you are looking inside that cast array.
And now you'd like to remove the foreach?
I just learned that whereJsonContains can take an array, just like whereIn.
See https://laravel.com/docs/5.7/queries#json-where-clauses
Could that be what you're looking for?
Please or to participate in this conversation.