Skip to content

Commit

Permalink
[9.x] Extend eloquent higher order proxy properties (#41449)
Browse files Browse the repository at this point in the history
* 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 <taylor@laravel.com>
  • Loading branch information
mikeydevelops and taylorotwell committed Mar 12, 2022
1 parent b4cbd6e commit 0a503ed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Expand Up @@ -22,6 +22,8 @@

/**
* @property-read HigherOrderBuilderProxy $orWhere
* @property-read HigherOrderBuilderProxy $whereNot
* @property-read HigherOrderBuilderProxy $orWhereNot
*
* @mixin \Illuminate\Database\Query\Builder
*/
Expand Down Expand Up @@ -1655,7 +1657,7 @@ public static function hasGlobalMacro($name)
*/
public function __get($key)
{
if ($key === 'orWhere') {
if (in_array($key, ['orWhere', 'whereNot', 'orWhereNot'])) {
return new HigherOrderBuilderProxy($this, $key);
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Expand Up @@ -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();
Expand Down

0 comments on commit 0a503ed

Please sign in to comment.