Skip to content
New issue

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

[11.x] Add whereNone method to the query builder #52260

Merged

Conversation

einar-hansen
Copy link
Contributor

@einar-hansen einar-hansen commented Jul 25, 2024

This pull request complements the work done by @musiermoore in the PR 50344 where the whereAny and whereAll methods were added to the framework. The new whereNone method is the often more forgotten "little sibling" / negative path function to complete this family of methods.

Similar to whereAny and whereAll, the whereNone method operates on multiple columns, but it uses negation to achieve the opposite effect of whereAny. This method gives users the ability to query for records where none of the specified columns match the given condition, thus completing the logical triad of "any", "all", and "none".

The whereNone method works as follows:

$users = DB::table('users')
            ->where('active', true)
            ->whereNone([
                'first_name',
                'last_name',
                'email',
            ], 'LIKE', 'einar%')
            ->get();

This will generate the following SQL:

SELECT *
FROM users
WHERE active = true AND NOT (
    first_name LIKE 'einar%' OR
    last_name LIKE 'einar%' OR
    email LIKE 'einar%'
)

By adding whereNone, we complete the set of methods for handling constraints across multiple columns.

@einar-hansen
Copy link
Contributor Author

I've also added a draft PR with documentation of the method to the laravel/docs repository.

@taylorotwell taylorotwell merged commit 8564950 into laravel:11.x Jul 29, 2024
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants