From a235578d32a102f40bbd841ef246312aa3129e79 Mon Sep 17 00:00:00 2001 From: Einar Hansen <49709354+einar-hansen@users.noreply.github.com> Date: Thu, 25 Jul 2024 00:50:58 +0200 Subject: [PATCH 1/2] Fix grammar --- src/Illuminate/Database/Query/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index e32d8b49e20c..ffe2238a8273 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -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 From 8f57b8686df816c40351fbf6faeca647bdb2de77 Mon Sep 17 00:00:00 2001 From: Einar Hansen <49709354+einar-hansen@users.noreply.github.com> Date: Thu, 25 Jul 2024 00:51:13 +0200 Subject: [PATCH 2/2] Add whereNone method the query builder --- src/Illuminate/Database/Query/Builder.php | 27 ++++++++++++++++ tests/Database/DatabaseQueryBuilderTest.php | 36 +++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index ffe2238a8273..90aedd542a38 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -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. * diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index c345ffcfb5c8..67f605eb9e32 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -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();