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

[11.x] Feature/whereany closures #52555

Merged
merged 7 commits into from
Aug 22, 2024
Merged
Prev Previous commit
Next Next commit
Modify Builder::whereAll and Builder::orWhereAll to allow passing an …
…array of closures
liamduckett committed Aug 22, 2024
commit b3a23fdf556061da70deae16e0e7df91c39066d4
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
@@ -2238,7 +2238,7 @@ public function orWhereFullText($columns, $value, array $options = [])
/**
* Add a "where" clause to the query for multiple columns with "and" conditions between them.
*
* @param \Illuminate\Contracts\Database\Query\Expression[]|string[] $columns
* @param \Illuminate\Contracts\Database\Query\Expression[]|string[]|\Closure[] $columns
* @param mixed $operator
* @param mixed $value
* @param string $boolean
@@ -2262,7 +2262,7 @@ public function whereAll($columns, $operator = null, $value = null, $boolean = '
/**
* Add an "or where" clause to the query for multiple columns with "and" conditions between them.
*
* @param \Illuminate\Contracts\Database\Query\Expression[]|string[] $columns
* @param \Illuminate\Contracts\Database\Query\Expression[]|string[]|\Closure[] $columns
* @param mixed $operator
* @param mixed $value
* @return $this
21 changes: 21 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1325,6 +1325,19 @@ public function testWhereAll()
$builder->select('*')->from('users')->whereAll(['last_name', 'email'], 'not like', '%Otwell%');
$this->assertSame('select * from "users" where ("last_name" not like ? and "email" not like ?)', $builder->toSql());
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereAll(['last_name', 'email'], '%Otwell%');
$this->assertSame('select * from "users" where ("last_name" = ? and "email" = ?)', $builder->toSql());
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereAll([
fn(Builder $query) => $query->where('last_name', 'like', '%Otwell%'),
fn(Builder $query) => $query->where('email', 'like', '%Otwell%'),
]);
$this->assertSame('select * from "users" where (("last_name" like ?) and ("email" like ?))', $builder->toSql());
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());
}

public function testOrWhereAll()
@@ -1343,6 +1356,14 @@ public function testOrWhereAll()
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereAll(['last_name', 'email'], '%Otwell%');
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" = ? and "email" = ?)', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereAll([
fn(Builder $query) => $query->where('last_name', 'like', '%Otwell%'),
fn(Builder $query) => $query->where('email', 'like', '%Otwell%'),
]);
$this->assertSame('select * from "users" where "first_name" like ? or (("last_name" like ?) and ("email" like ?))', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
}

public function testWhereAny()