Skip to content

[12.x] Add WhereNotAny, WhereNotAll methods to Query Builder #55291

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2335,21 +2335,75 @@ 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.
* Add a "where not" clause to the query for multiple columns with "or" conditions between them.
*
* @param \Illuminate\Contracts\Database\Query\Expression[]|\Closure[]|string[] $columns
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return $this
*/
public function whereNone($columns, $operator = null, $value = null, $boolean = 'and')
public function whereNotAny($columns, $operator = null, $value = null, $boolean = 'and')
{
return $this->whereAny($columns, $operator, $value, $boolean.' not');
}

/**
* Add an "or where not" clause to the query for multiple columns where none of the conditions should be true.
* Add an "or where not" clause to the query for multiple columns with "or" conditions between them.
*
* @param \Illuminate\Contracts\Database\Query\Expression[]|\Closure[]|string[] $columns
* @param mixed $operator
* @param mixed $value
* @return $this
*/
public function orWhereNotAny($columns, $operator = null, $value = null)
{
return $this->whereNotAny($columns, $operator, $value, 'or');
}

/**
* Add a "where not" clause to the query for multiple columns with "and" conditions between them.
*
* @param \Illuminate\Contracts\Database\Query\Expression[]|\Closure[]|string[] $columns
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return $this
*/
public function whereNotAll($columns, $operator = null, $value = null, $boolean = 'and')
{
return $this->whereAll($columns, $operator, $value, $boolean.' not');
}

/**
* Add an "or where not" clause to the query for multiple columns with "and" conditions between them.
*
* @param \Illuminate\Contracts\Database\Query\Expression[]|\Closure[]|string[] $columns
* @param mixed $operator
* @param mixed $value
* @return $this
*/
public function orWhereNotAll($columns, $operator = null, $value = null)
{
return $this->whereNotAll($columns, $operator, $value, 'or');
}

/**
* Alias for the "whereNotAny" method.
*
* @param \Illuminate\Contracts\Database\Query\Expression[]|\Closure[]|string[] $columns
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return $this
*/
public function whereNone($columns, $operator = null, $value = null, $boolean = 'and')
{
return $this->whereNotAny(...func_get_args());
}

/**
* Alias for the "orWhereNotAny" method.
*
* @param \Illuminate\Contracts\Database\Query\Expression[]|\Closure[]|string[] $columns
* @param mixed $operator
Expand All @@ -2358,7 +2412,7 @@ public function whereNone($columns, $operator = null, $value = null, $boolean =
*/
public function orWhereNone($columns, $operator = null, $value = null)
{
return $this->whereNone($columns, $operator, $value, 'or');
return $this->orWhereNotAny(...func_get_args());
}

/**
Expand Down
92 changes: 92 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,98 @@ public function testOrWhereAny()
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
}

public function testWhereNotAny()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNotAny(['last_name', 'email'], 'like', '%Otwell%');
$this->assertSame('select * from "users" where not ("last_name" like ? or "email" like ?)', $builder->toSql());
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->whereNotAny(['email_verified_at', 'deleted_at'], null);
$this->assertSame('select * from "users" where "first_name" like ? and not ("email_verified_at" is null or "deleted_at" is null)', $builder->toSql());
$this->assertEquals(['%Taylor%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->whereNotAny(['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')->whereNotAny(['email_verified_at', 'deleted_at'], null);
$this->assertSame('select * from "users" where not ("email_verified_at" is null or "deleted_at" is null)', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}

public function testOrWhereNotAny()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereNotAny(['last_name', '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());

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

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

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->whereNotAll(['email_verified_at', 'deleted_at'], null);
$this->assertSame('select * from "users" where "first_name" like ? and not ("email_verified_at" is null and "deleted_at" is null)', $builder->toSql());
$this->assertEquals(['%Taylor%'], $builder->getBindings());

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

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNotAll(['email_verified_at', 'deleted_at'], null);
$this->assertSame('select * from "users" where not ("email_verified_at" is null and "deleted_at" is null)', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}

public function testOrWhereNotAll()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereNotAll(['last_name', 'email'], 'like', '%Otwell%');
$this->assertSame('select * from "users" where "first_name" like ? or not ("last_name" like ? and "email" like ?)', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());

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

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

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