Skip to content

Commit

Permalink
[11.x] Feature/whereany closures (#52555)
Browse files Browse the repository at this point in the history
* Modify Builder::whereAny to allow passing an array of closures

* Add test for Builder::whereAny passing an array of closures

* Modify Builder::orWhereAny to allow passing an array of closures

* Modify Builder::whereNone and Builder::orWhereNone to allow passing an array of closures

* Modify Builder::whereAll and Builder::orWhereAll to allow passing an array of closures

* Remove duplicated test

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
liamduckett and taylorotwell authored Aug 22, 2024
1 parent b11c240 commit 738bc11
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]|\Closure[]|string[] $columns
* @param mixed $operator
* @param mixed $value
* @param string $boolean
Expand All @@ -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[]|\Closure[]|string[] $columns
* @param mixed $operator
* @param mixed $value
* @return $this
Expand All @@ -2275,7 +2275,7 @@ public function orWhereAll($columns, $operator = null, $value = null)
/**
* Add a "where" clause to the query for multiple columns with "or" conditions between them.
*
* @param \Illuminate\Contracts\Database\Query\Expression[]|string[] $columns
* @param \Illuminate\Contracts\Database\Query\Expression[]|\Closure[]|string[] $columns
* @param mixed $operator
* @param mixed $value
* @param string $boolean
Expand All @@ -2299,7 +2299,7 @@ public function whereAny($columns, $operator = null, $value = null, $boolean = '
/**
* Add an "or where" clause to the query for multiple columns with "or" conditions between them.
*
* @param \Illuminate\Contracts\Database\Query\Expression[]|string[] $columns
* @param \Illuminate\Contracts\Database\Query\Expression[]|\Closure[]|string[] $columns
* @param mixed $operator
* @param mixed $value
* @return $this
Expand All @@ -2312,7 +2312,7 @@ public function orWhereAny($columns, $operator = null, $value = null)
/**
* Add a "where not" clause to the query for multiple columns where none of the conditions should be true.
*
* @param \Illuminate\Contracts\Database\Query\Expression[]|string[] $columns
* @param \Illuminate\Contracts\Database\Query\Expression[]|\Closure[]|string[] $columns
* @param mixed $operator
* @param mixed $value
* @param string $boolean
Expand All @@ -2326,7 +2326,7 @@ public function whereNone($columns, $operator = null, $value = null, $boolean =
/**
* Add an "or where not" clause to the query for multiple columns where none of the conditions should be true.
*
* @param \Illuminate\Contracts\Database\Query\Expression[]|string[] $columns
* @param \Illuminate\Contracts\Database\Query\Expression[]|\Closure[]|string[] $columns
* @param mixed $operator
* @param mixed $value
* @return $this
Expand Down
48 changes: 48 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,14 @@ 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([
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()
Expand All @@ -1343,6 +1351,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()
Expand All @@ -1356,6 +1372,14 @@ public function testWhereAny()
$builder->select('*')->from('users')->whereAny(['last_name', 'email'], '%Otwell%');
$this->assertSame('select * from "users" where ("last_name" = ? or "email" = ?)', $builder->toSql());
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereAny([
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 ?) or ("email" like ?))', $builder->toSql());
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());
}

public function testOrWhereAny()
Expand All @@ -1374,6 +1398,14 @@ public function testOrWhereAny()
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereAny(['last_name', 'email'], '%Otwell%');
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" = ? or "email" = ?)', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereAny([
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 ?) or ("email" like ?))', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
}

public function testWhereNone()
Expand All @@ -1392,6 +1424,14 @@ public function testWhereNone()
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->whereNone(['last_name', 'email'], 'like', '%Otwell%');
$this->assertSame('select * from "users" where "first_name" like ? and not ("last_name" like ? or "email" like ?)', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());

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

public function testOrWhereNone()
Expand All @@ -1410,6 +1450,14 @@ public function testOrWhereNone()
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereNone(['last_name', 'email'], '%Otwell%');
$this->assertSame('select * from "users" where "first_name" like ? or not ("last_name" = ? or "email" = ?)', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereNone([
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 not (("last_name" like ?) or ("email" like ?))', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
}

public function testUnions()
Expand Down

0 comments on commit 738bc11

Please sign in to comment.