Skip to content

Commit 8564950

Browse files
authored
[11.x] Add whereNone method to the query builder (#52260)
* Fix grammar * Add whereNone method the query builder
1 parent 97fe718 commit 8564950

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/Illuminate/Database/Query/Builder.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2273,7 +2273,7 @@ public function orWhereAll($columns, $operator = null, $value = null)
22732273
}
22742274

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

2312+
/**
2313+
* Add a "where not" clause to the query for multiple columns where none of the conditions should be true.
2314+
*
2315+
* @param string[] $columns
2316+
* @param string $operator
2317+
* @param mixed $value
2318+
* @param string $boolean
2319+
* @return $this
2320+
*/
2321+
public function whereNone($columns, $operator = null, $value = null, $boolean = 'and')
2322+
{
2323+
return $this->whereAny($columns, $operator, $value, $boolean.' not');
2324+
}
2325+
2326+
/**
2327+
* Add an "or where not" clause to the query for multiple columns where none of the conditions should be true.
2328+
*
2329+
* @param string[] $columns
2330+
* @param string $operator
2331+
* @param mixed $value
2332+
* @return $this
2333+
*/
2334+
public function orWhereNone($columns, $operator = null, $value = null)
2335+
{
2336+
return $this->whereNone($columns, $operator, $value, 'or');
2337+
}
2338+
23122339
/**
23132340
* Add a "group by" clause to the query.
23142341
*

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,42 @@ public function testOrWhereAny()
13761376
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
13771377
}
13781378

1379+
public function testWhereNone()
1380+
{
1381+
$builder = $this->getBuilder();
1382+
$builder->select('*')->from('users')->whereNone(['last_name', 'email'], 'like', '%Otwell%');
1383+
$this->assertSame('select * from "users" where not ("last_name" like ? or "email" like ?)', $builder->toSql());
1384+
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());
1385+
1386+
$builder = $this->getBuilder();
1387+
$builder->select('*')->from('users')->whereNone(['last_name', 'email'], '%Otwell%');
1388+
$this->assertSame('select * from "users" where not ("last_name" = ? or "email" = ?)', $builder->toSql());
1389+
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());
1390+
1391+
$builder = $this->getBuilder();
1392+
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->whereNone(['last_name', 'email'], 'like', '%Otwell%');
1393+
$this->assertSame('select * from "users" where "first_name" like ? and not ("last_name" like ? or "email" like ?)', $builder->toSql());
1394+
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
1395+
}
1396+
1397+
public function testOrWhereNone()
1398+
{
1399+
$builder = $this->getBuilder();
1400+
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereNone(['last_name', 'email'], 'like', '%Otwell%');
1401+
$this->assertSame('select * from "users" where "first_name" like ? or not ("last_name" like ? or "email" like ?)', $builder->toSql());
1402+
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
1403+
1404+
$builder = $this->getBuilder();
1405+
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->whereNone(['last_name', 'email'], 'like', '%Otwell%', 'or');
1406+
$this->assertSame('select * from "users" where "first_name" like ? or not ("last_name" like ? or "email" like ?)', $builder->toSql());
1407+
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
1408+
1409+
$builder = $this->getBuilder();
1410+
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereNone(['last_name', 'email'], '%Otwell%');
1411+
$this->assertSame('select * from "users" where "first_name" like ? or not ("last_name" = ? or "email" = ?)', $builder->toSql());
1412+
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
1413+
}
1414+
13791415
public function testUnions()
13801416
{
13811417
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)