From 88adf85686cedd2fd1a7243a54d9453f86a9fb46 Mon Sep 17 00:00:00 2001 From: Dmytro Kulyk Date: Thu, 3 Oct 2019 19:28:49 +0300 Subject: [PATCH] [6.x] Allow to use scoped macro in nested queries (#30127) * Allow to use scoped macro in nested queries. * Use newQueryWithoutRelationships --- src/Illuminate/Database/Eloquent/Builder.php | 2 +- tests/Database/DatabaseEloquentBuilderTest.php | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 9b88196972f9..a28db3fc99c9 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -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 { diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 0783124c5f22..802424a66d44 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -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); @@ -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;