Skip to content

Commit

Permalink
Ensure where with array respects boolean (laravel#53147)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Oct 15, 2024
1 parent 5c9dc3b commit 8c286a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ 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)) {
$query->{$method}(...array_values($value));
$query->{$method}(...array_values($value), boolean: $boolean);
} else {
$query->{$method}($key, '=', $value, $boolean);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2323,15 +2323,30 @@ public function testWhereWithArrayConditions()
$this->assertSame('select * from "users" where ("foo" = ? and "bar" = ?)', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());

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

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

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

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

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

public function testNestedWheres()
Expand Down

0 comments on commit 8c286a2

Please sign in to comment.