From 075823fd75172d906f101466f871b37776565db5 Mon Sep 17 00:00:00 2001 From: Michael Goodman Date: Fri, 11 Mar 2022 21:52:11 +0200 Subject: [PATCH 1/4] Added easier way to define which properties should be passed thru the eloquent builder. --- src/Illuminate/Database/Eloquent/Builder.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 830f54f860f7..6d12ca893d40 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -82,6 +82,15 @@ class Builder implements BuilderContract 'from', ]; + /** + * The properties that should be proxied to query builder. + * + * @var string[] + */ + protected $higherOrderPassthru = [ + 'orWhere', + ]; + /** * The methods that should be returned from query builder. * @@ -1655,7 +1664,7 @@ public static function hasGlobalMacro($name) */ public function __get($key) { - if ($key === 'orWhere') { + if (in_array($key, $this->higherOrderPassthru)) { return new HigherOrderBuilderProxy($this, $key); } From 6628073ce799d644c49219462068d57f66404fd3 Mon Sep 17 00:00:00 2001 From: Michael Goodman Date: Fri, 11 Mar 2022 21:53:49 +0200 Subject: [PATCH 2/4] Enabled whereNot and orWhereNot methods to be higher order proxied in the eloquent builder. --- src/Illuminate/Database/Eloquent/Builder.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 6d12ca893d40..4729727ca26a 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -22,6 +22,8 @@ /** * @property-read HigherOrderBuilderProxy $orWhere + * @property-read HigherOrderBuilderProxy $whereNot + * @property-read HigherOrderBuilderProxy $orWhereNot * * @mixin \Illuminate\Database\Query\Builder */ @@ -89,6 +91,8 @@ class Builder implements BuilderContract */ protected $higherOrderPassthru = [ 'orWhere', + 'whereNot', + 'orWhereNot', ]; /** From 6ad4db169b8c0e481706049feeb498a6d63649dd Mon Sep 17 00:00:00 2001 From: Michael Goodman Date: Sat, 12 Mar 2022 02:08:51 +0200 Subject: [PATCH 3/4] added tests for hiher order proxy methods --- .../Database/DatabaseEloquentBuilderTest.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 7c67368689e1..fc22a94eb3f4 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -956,6 +956,38 @@ public function testRealQueryChainedHigherOrderOrWhereScopes() $this->assertSame('select * from "table" where "one" = ? or ("two" = ?) or ("three" = ?)', $query->toSql()); } + public function testRealQueryHigherOrderWhereNotScopes() + { + $model = new EloquentBuilderTestHigherOrderWhereScopeStub; + $this->mockConnectionForModel($model, 'SQLite'); + $query = $model->newQuery()->one()->whereNot->two(); + $this->assertSame('select * from "table" where "one" = ? and not ("two" = ?)', $query->toSql()); + } + + public function testRealQueryChainedHigherOrderWhereNotScopes() + { + $model = new EloquentBuilderTestHigherOrderWhereScopeStub; + $this->mockConnectionForModel($model, 'SQLite'); + $query = $model->newQuery()->one()->whereNot->two()->whereNot->three(); + $this->assertSame('select * from "table" where "one" = ? and not ("two" = ?) and not ("three" = ?)', $query->toSql()); + } + + public function testRealQueryHigherOrderOrWhereNotScopes() + { + $model = new EloquentBuilderTestHigherOrderWhereScopeStub; + $this->mockConnectionForModel($model, 'SQLite'); + $query = $model->newQuery()->one()->orWhereNot->two(); + $this->assertSame('select * from "table" where "one" = ? or not ("two" = ?)', $query->toSql()); + } + + public function testRealQueryChainedHigherOrderOrWhereNotScopes() + { + $model = new EloquentBuilderTestHigherOrderWhereScopeStub; + $this->mockConnectionForModel($model, 'SQLite'); + $query = $model->newQuery()->one()->orWhereNot->two()->orWhereNot->three(); + $this->assertSame('select * from "table" where "one" = ? or not ("two" = ?) or not ("three" = ?)', $query->toSql()); + } + public function testSimpleWhere() { $builder = $this->getBuilder(); From 7c1edde54c5d7b25008a526441713f91088a89d3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 12 Mar 2022 11:17:14 -0600 Subject: [PATCH 4/4] formatting --- src/Illuminate/Database/Eloquent/Builder.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 4729727ca26a..f93b217f9bc7 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -84,17 +84,6 @@ class Builder implements BuilderContract 'from', ]; - /** - * The properties that should be proxied to query builder. - * - * @var string[] - */ - protected $higherOrderPassthru = [ - 'orWhere', - 'whereNot', - 'orWhereNot', - ]; - /** * The methods that should be returned from query builder. * @@ -1668,7 +1657,7 @@ public static function hasGlobalMacro($name) */ public function __get($key) { - if (in_array($key, $this->higherOrderPassthru)) { + if (in_array($key, ['orWhere', 'whereNot', 'orWhereNot'])) { return new HigherOrderBuilderProxy($this, $key); }