Skip to content

Commit 0a503ed

Browse files
[9.x] Extend eloquent higher order proxy properties (#41449)
* Added easier way to define which properties should be passed thru the eloquent builder. * Enabled whereNot and orWhereNot methods to be higher order proxied in the eloquent builder. * added tests for hiher order proxy methods * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent b4cbd6e commit 0a503ed

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
/**
2424
* @property-read HigherOrderBuilderProxy $orWhere
25+
* @property-read HigherOrderBuilderProxy $whereNot
26+
* @property-read HigherOrderBuilderProxy $orWhereNot
2527
*
2628
* @mixin \Illuminate\Database\Query\Builder
2729
*/
@@ -1655,7 +1657,7 @@ public static function hasGlobalMacro($name)
16551657
*/
16561658
public function __get($key)
16571659
{
1658-
if ($key === 'orWhere') {
1660+
if (in_array($key, ['orWhere', 'whereNot', 'orWhereNot'])) {
16591661
return new HigherOrderBuilderProxy($this, $key);
16601662
}
16611663

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,38 @@ public function testRealQueryChainedHigherOrderOrWhereScopes()
956956
$this->assertSame('select * from "table" where "one" = ? or ("two" = ?) or ("three" = ?)', $query->toSql());
957957
}
958958

959+
public function testRealQueryHigherOrderWhereNotScopes()
960+
{
961+
$model = new EloquentBuilderTestHigherOrderWhereScopeStub;
962+
$this->mockConnectionForModel($model, 'SQLite');
963+
$query = $model->newQuery()->one()->whereNot->two();
964+
$this->assertSame('select * from "table" where "one" = ? and not ("two" = ?)', $query->toSql());
965+
}
966+
967+
public function testRealQueryChainedHigherOrderWhereNotScopes()
968+
{
969+
$model = new EloquentBuilderTestHigherOrderWhereScopeStub;
970+
$this->mockConnectionForModel($model, 'SQLite');
971+
$query = $model->newQuery()->one()->whereNot->two()->whereNot->three();
972+
$this->assertSame('select * from "table" where "one" = ? and not ("two" = ?) and not ("three" = ?)', $query->toSql());
973+
}
974+
975+
public function testRealQueryHigherOrderOrWhereNotScopes()
976+
{
977+
$model = new EloquentBuilderTestHigherOrderWhereScopeStub;
978+
$this->mockConnectionForModel($model, 'SQLite');
979+
$query = $model->newQuery()->one()->orWhereNot->two();
980+
$this->assertSame('select * from "table" where "one" = ? or not ("two" = ?)', $query->toSql());
981+
}
982+
983+
public function testRealQueryChainedHigherOrderOrWhereNotScopes()
984+
{
985+
$model = new EloquentBuilderTestHigherOrderWhereScopeStub;
986+
$this->mockConnectionForModel($model, 'SQLite');
987+
$query = $model->newQuery()->one()->orWhereNot->two()->orWhereNot->three();
988+
$this->assertSame('select * from "table" where "one" = ? or not ("two" = ?) or not ("three" = ?)', $query->toSql());
989+
}
990+
959991
public function testSimpleWhere()
960992
{
961993
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)