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] Ensure where with array respects boolean #53147

Merged
merged 1 commit into from
Oct 14, 2024

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Oct 13, 2024

It is possible to create where queries with an array. The array may be a hash map or a list.

User::where([
    'name' => 'Tim',
    'verified' => true,
]);

// select * from users where (name = 'Tim' and verified = 1)

User::where([
    ['name', 'Tim'],
    ['created_at', '>', now()->startOfYear()->toDateString()],
]);

// select * from users where (name = 'Tim' and created_at > '2024-01-01')

There where method accepts a boolean for the query, e.g., AND, OR. The boolean is respected for the hash map but is ignored for the list.

User::where([
    'name' => 'Tim',
    'verified' => true,
], boolean: 'or');

// select * from users where (name = 'Tim' or verified = 1)

User::where([
    ['name', 'Tim'],
    ['created_at', '>', now()->startOfYear()],
], boolean: 'or');

// select * from users where (name = 'Tim' and created_at > '2024-01-01')

This PR ensures that the boolean is respected for both approaches.

User::where([
    'name' => 'Tim',
    'verified' => true,
], boolean: 'or');

// select * from users where (name = 'Tim' or verified = 1)

User::where([
    ['name', 'Tim'],
    ['created_at', '>', now()->startOfYear()],
], boolean: 'or');

- // select * from users where (name = 'Tim' and created_at > '2024-01-01')
+ // select * from users where (name = 'Tim' or created_at > '2024-01-01')

@taylorotwell taylorotwell merged commit d62e92d into laravel:11.x Oct 14, 2024
33 checks passed
timacdonald added a commit to timacdonald/framework that referenced this pull request Oct 15, 2024
@timacdonald timacdonald deleted the where-array-boolean branch October 15, 2024 00:14
taylorotwell added a commit that referenced this pull request Oct 16, 2024
timacdonald added a commit to timacdonald/framework that referenced this pull request Oct 17, 2024
@Yinci
Copy link

Yinci commented Dec 13, 2024

I am currently working on an upgrade to L11, and came across the exception Named parameter $boolean overwrites previous argument. I was using the 4th argument in the array of wheres to pass a boolean to the where. With this change, it no longer works. Using a simple array dataset to craft a logical combined and/or flow no longer works. Is there an available workaround or is the only option to fully deconstruct the array and manually create the flow?

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.

3 participants