We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hello there! I think that the implementation of the whereNotIn method is very helpful,
whereNotIn
User::search($search)->whereNotIn('id', [1, 2, 3]);
Let’s think that we need to search for a User, but filter them by $except & $only.
$except
$only
If we want to apply only the except filter, we can perform two queries to get the results
User::search($search) ->when($except, function (Builder $query) use ($except, $only) { $ids = User::whereNotIn('id', $except)->pluck('id')->toArray(); $query->whereIn('id', $ids); }) ->take(5) ->get();
But the things get harder when we want to combine the $except and the $only
User::search($search) ->when($except || $only, function (Builder $query) use ($except, $only) { $except = !$only && $except ? User::whereNotIn('id', $except)->pluck('id')->toArray() : $except ?? []; $values = is_array($only) ? array_diff($only, $except) : $except; $query->whereIn('id', $values); }) ->take(5) ->get();
With the whereNotIn, we can combine both
User::search($search) ->when($except, fn (Builder $query) => $query->whereNotIn('id', $except)) ->when($only, fn (Builder $query) => $query->whereIn('id', $only)) ->take(5) ->get();
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Description
Hello there! I think that the implementation of the
whereNotIn
method is very helpful,Let’s think that we need to search for a User, but filter them by
$except
&$only
.If we want to apply only the except filter, we can perform two queries to get the results
But the things get harder when we want to combine the
$except
and the$only
With the
whereNotIn
, we can combine bothThe text was updated successfully, but these errors were encountered: