Skip to content

Commit

Permalink
Fix QueryBuilder whereNot with array conditions (#44083)
Browse files Browse the repository at this point in the history
* bugfix

* consequences

* boyscout rule

* styleci

* narrow surface area

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
Propaganistas and taylorotwell authored Sep 12, 2022
1 parent f310c74 commit 75ec9c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ protected function addArrayOfWheres($column, $boolean, $method = 'where')
if (is_numeric($key) && is_array($value)) {
$query->{$method}(...array_values($value));
} else {
$query->$method($key, '=', $value, $boolean);
$query->{$method}($key, '=', $value, $boolean);
}
}
}, $boolean);
Expand Down Expand Up @@ -894,6 +894,12 @@ public function orWhere($column, $operator = null, $value = null)
*/
public function whereNot($column, $operator = null, $value = null, $boolean = 'and')
{
if (is_array($column)) {
return $this->whereNested(function ($query) use ($column, $operator, $value, $boolean) {
$query->where($column, $operator, $value, $boolean);
}, $boolean.' not');
}

return $this->where($column, $operator, $value, $boolean.' not');
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,24 @@ public function testWhereNot()
$this->assertEquals([0 => 'bar', 1 => 'foo'], $builder->getBindings());
}

public function testWhereNotWithArrayConditions()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNot([['foo', 1], ['bar', 2]]);
$this->assertSame('select * from "users" where not (("foo" = ? and "bar" = ?))', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNot(['foo' => 1, 'bar' => 2]);
$this->assertSame('select * from "users" where not (("foo" = ? and "bar" = ?))', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNot([['foo', 1], ['bar', '<', 2]]);
$this->assertSame('select * from "users" where not (("foo" = ? and "bar" < ?))', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
}

public function testFullSubSelects()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit 75ec9c3

Please sign in to comment.