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

[Eloquent Bug] orWhere with array conditions generates incorrect SQL (OR instead of grouped AND) #54391

Closed
vnicorici opened this issue Jan 28, 2025 · 1 comment

Comments

@vnicorici
Copy link

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:

DB::table('matches')
    ->where([
        ['participant_1', '=', '3b0b9ef4'],
        ['participant_2', '=', 'c2a69968'],
    ])
    ->orWhere([
        ['participant_1', '=', 'c2a69968'],
        ['participant_2', '=', '3b0b9ef4'],
    ])
    ->toSql();

Expected SQL (grouping each array with AND):

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:

DB::table('matches')
    ->where(function ($query) {
        $query->where('participant_1', '3b0b9ef4')
              ->where('participant_2', 'c2a69968');
    })
    ->orWhere(function ($query) {
        $query->where('participant_1', 'c2a69968')
              ->where('participant_2', '3b0b9ef4');
    })
    ->get();

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.

@hafezdivandari
Copy link
Contributor

Duplicate of #53628 ?

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

No branches or pull requests

2 participants