Skip to content

Commit

Permalink
[11.x] Add whereNone method to the query builder (#52260)
Browse files Browse the repository at this point in the history
* Fix grammar

* Add whereNone method the query builder
  • Loading branch information
einar-hansen authored Jul 29, 2024
1 parent 97fe718 commit 8564950
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2273,7 +2273,7 @@ public function orWhereAll($columns, $operator = null, $value = null)
}

/**
* Add an "where" clause to the query for multiple columns with "or" conditions between them.
* Add a "where" clause to the query for multiple columns with "or" conditions between them.
*
* @param string[] $columns
* @param string $operator
Expand Down Expand Up @@ -2309,6 +2309,33 @@ public function orWhereAny($columns, $operator = null, $value = null)
return $this->whereAny($columns, $operator, $value, 'or');
}

/**
* Add a "where not" clause to the query for multiple columns where none of the conditions should be true.
*
* @param string[] $columns
* @param string $operator
* @param mixed $value
* @param string $boolean
* @return $this
*/
public function whereNone($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.
*
* @param string[] $columns
* @param string $operator
* @param mixed $value
* @return $this
*/
public function orWhereNone($columns, $operator = null, $value = null)
{
return $this->whereNone($columns, $operator, $value, 'or');
}

/**
* Add a "group by" clause to the query.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,42 @@ public function testOrWhereAny()
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
}

public function testWhereNone()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNone(['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')->whereNone(['last_name', 'email'], '%Otwell%');
$this->assertSame('select * from "users" where not ("last_name" = ? or "email" = ?)', $builder->toSql());
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$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());
}

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

$builder = $this->getBuilder();
$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());
}

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

0 comments on commit 8564950

Please sign in to comment.