Skip to content

Commit 75ec9c3

Browse files
Fix QueryBuilder whereNot with array conditions (#44083)
* bugfix * consequences * boyscout rule * styleci * narrow surface area Co-authored-by: Taylor Otwell <[email protected]>
1 parent f310c74 commit 75ec9c3

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Illuminate/Database/Query/Builder.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ protected function addArrayOfWheres($column, $boolean, $method = 'where')
800800
if (is_numeric($key) && is_array($value)) {
801801
$query->{$method}(...array_values($value));
802802
} else {
803-
$query->$method($key, '=', $value, $boolean);
803+
$query->{$method}($key, '=', $value, $boolean);
804804
}
805805
}
806806
}, $boolean);
@@ -894,6 +894,12 @@ public function orWhere($column, $operator = null, $value = null)
894894
*/
895895
public function whereNot($column, $operator = null, $value = null, $boolean = 'and')
896896
{
897+
if (is_array($column)) {
898+
return $this->whereNested(function ($query) use ($column, $operator, $value, $boolean) {
899+
$query->where($column, $operator, $value, $boolean);
900+
}, $boolean.' not');
901+
}
902+
897903
return $this->where($column, $operator, $value, $boolean.' not');
898904
}
899905

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,24 @@ public function testWhereNot()
17481748
$this->assertEquals([0 => 'bar', 1 => 'foo'], $builder->getBindings());
17491749
}
17501750

1751+
public function testWhereNotWithArrayConditions()
1752+
{
1753+
$builder = $this->getBuilder();
1754+
$builder->select('*')->from('users')->whereNot([['foo', 1], ['bar', 2]]);
1755+
$this->assertSame('select * from "users" where not (("foo" = ? and "bar" = ?))', $builder->toSql());
1756+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
1757+
1758+
$builder = $this->getBuilder();
1759+
$builder->select('*')->from('users')->whereNot(['foo' => 1, 'bar' => 2]);
1760+
$this->assertSame('select * from "users" where not (("foo" = ? and "bar" = ?))', $builder->toSql());
1761+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
1762+
1763+
$builder = $this->getBuilder();
1764+
$builder->select('*')->from('users')->whereNot([['foo', 1], ['bar', '<', 2]]);
1765+
$this->assertSame('select * from "users" where not (("foo" = ? and "bar" < ?))', $builder->toSql());
1766+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
1767+
}
1768+
17511769
public function testFullSubSelects()
17521770
{
17531771
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)