Skip to content

Commit

Permalink
[6.x] Allow to use scoped macro in nested queries (#30127)
Browse files Browse the repository at this point in the history
* Allow to use scoped macro in nested queries.

* Use newQueryWithoutRelationships
  • Loading branch information
Dmytro Kulyk authored and taylorotwell committed Oct 3, 2019
1 parent 7109d5f commit 88adf85
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function whereKeyNot($id)
public function where($column, $operator = null, $value = null, $boolean = 'and')
{
if ($column instanceof Closure) {
$column($query = $this->model->newModelQuery());
$column($query = $this->model->newQueryWithoutRelationships());

$this->query->addNestedWhereQuery($query->getQuery(), $boolean);
} else {
Expand Down
13 changes: 12 additions & 1 deletion tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ public function testNestedWhere()
$nestedRawQuery = $this->getMockQueryBuilder();
$nestedQuery->shouldReceive('getQuery')->once()->andReturn($nestedRawQuery);
$model = $this->getMockModel()->makePartial();
$model->shouldReceive('newModelQuery')->once()->andReturn($nestedQuery);
$model->shouldReceive('newQueryWithoutRelationships')->once()->andReturn($nestedQuery);
$builder = $this->getBuilder();
$builder->getQuery()->shouldReceive('from');
$builder->setModel($model);
Expand All @@ -667,6 +667,17 @@ public function testRealNestedWhereWithScopes()
$this->assertEquals(['bar', 9000], $query->getBindings());
}

public function testRealNestedWhereWithScopesMacro()
{
$model = new EloquentBuilderTestNestedStub;
$this->mockConnectionForModel($model, 'SQLite');
$query = $model->newQuery()->where('foo', '=', 'bar')->where(function ($query) {
$query->where('baz', '>', 9000)->onlyTrashed();
})->withTrashed();
$this->assertSame('select * from "table" where "foo" = ? and ("baz" > ? and "table"."deleted_at" is not null)', $query->toSql());
$this->assertEquals(['bar', 9000], $query->getBindings());
}

public function testRealNestedWhereWithMultipleScopesAndOneDeadScope()
{
$model = new EloquentBuilderTestNestedStub;
Expand Down

0 comments on commit 88adf85

Please sign in to comment.