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

orWhere([...]) with array conditions is not working #26962

Closed
leonma opened this issue Dec 27, 2018 · 3 comments
Closed

orWhere([...]) with array conditions is not working #26962

leonma opened this issue Dec 27, 2018 · 3 comments

Comments

@leonma
Copy link

leonma commented Dec 27, 2018

  • Laravel Version: 5.7.*
  • PHP Version: 7
  • Database Driver & Version:

Description:

When use the orWhere function with the array params, the result will be 'and'.

Steps To Reproduce:

$conditions = [['name', 'like', 'a%'], ['name', 'like', 'b%']];
User::orWhere($conditions)->toSql();
// should be: select * from users where (name like 'a%' or name like 'b%') and ...
// now: select * from users where (name like 'a%' and name like 'b%') and ...

Details

File: Illuminate\Database\Query\Builder.php
Line: 658-669

    protected function addArrayOfWheres($column, $boolean, $method = 'where')
    {
        return $this->whereNested(function ($query) use ($column, $method, $boolean) {
            foreach ($column as $key => $value) {
                if (is_numeric($key) && is_array($value)) {
                    if (count($value) < 4) {
                        $value[3] = $boolean
                    }
                    $query->$method(...array_values($value)); // here may be missing the $boolean param
                } else {
                    $query->$method($key, '=', $value, $boolean);
                }
            }
        }, $boolean);
    }

I think the $value[3] should be equal to $boolean if the array condition size is 3.

@staudenmeir
Copy link
Contributor

The behavior of orWhere() is quite inconsistent: #24755

@DarkGhostHunter
Copy link
Contributor

Should be able to add a orWhereAnd() method?

\App\User::where([
    ['name', 'Taylor']
])->orWhereAnd([
    ['genre', 'male'],
    ['role', 'developer'],
]);  
select * from users where name = 'Taylor' or where name = 'John' and where genre = 'male' and where role = 'developer'

@driesvints
Copy link
Member

I think it's best to use the suggestion from #24755 (comment) for now and post an issue at the laravel/ideas repo if you want this behavior to be changed.

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

5 participants
@driesvints @staudenmeir @leonma @DarkGhostHunter and others