You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using ->orWhere() with an array of multiple conditions, Eloquent appears to split the conditions and connect them with OR instead of grouping them with AND. For example:
select*from`matches`where (`participant_1`= ? and`participant_2`= ?)
or (`participant_1`= ? and`participant_2`= ?)
Actual SQL (output from ->toSql()):
select*from`matches`where (`participant_1`= ? and`participant_2`= ?)
or (`participant_1`= ? or`participant_2`= ?)
The bindings confirm that the second part of the condition uses OR for each item instead of AND.
Why This Is an Issue
• where([...]) properly interprets the array as multiple AND statements.
• orWhere([...]), however, seems to break these conditions into (condition1 OR condition2), rather than grouping them in an AND context within the overall OR.
This leads to unexpected and incorrect query results. Instead of returning rows that match both conditions in the second array, the query returns any row that meets either condition.
Steps To Reproduce
1. Create a table (e.g. matches) with columns participant_1 and participant_2.
2. Insert a few rows with different participant values.
3. Use the Eloquent snippet above and check ->toSql() / ->getBindings().
Workaround
A functional workaround is using closures to explicitly group conditions:
In that scenario, the generated SQL is correctly grouped:
where
(participant_1 = ? and participant_2 = ?)
or
(participant_1 = ? and participant_2 = ?)
Question / Proposal
• Is this behavior intentional (and simply not documented), or is it a bug in how orWhere([...]) handles arrays of conditions?
• Ideally, where([...]) and orWhere([...]) would both interpret their arrays consistently, grouping conditions with AND inside each array.
The text was updated successfully, but these errors were encountered:
Laravel Version
11.0.0
PHP Version
8.2.27
Database Driver & Version
No response
Description
When using ->orWhere() with an array of multiple conditions, Eloquent appears to split the conditions and connect them with OR instead of grouping them with AND. For example:
Expected SQL (grouping each array with AND):
Actual SQL (output from ->toSql()):
The bindings confirm that the second part of the condition uses OR for each item instead of AND.
Why This Is an Issue
• where([...]) properly interprets the array as multiple AND statements.
• orWhere([...]), however, seems to break these conditions into (condition1 OR condition2), rather than grouping them in an AND context within the overall OR.
This leads to unexpected and incorrect query results. Instead of returning rows that match both conditions in the second array, the query returns any row that meets either condition.
Steps To Reproduce
Workaround
A functional workaround is using closures to explicitly group conditions:
In that scenario, the generated SQL is correctly grouped:
Question / Proposal
• Is this behavior intentional (and simply not documented), or is it a bug in how orWhere([...]) handles arrays of conditions?
• Ideally, where([...]) and orWhere([...]) would both interpret their arrays consistently, grouping conditions with AND inside each array.
The text was updated successfully, but these errors were encountered: