From 69d9bcafb0dc926a0e242e20f4ef25896245e9a2 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Tue, 8 Nov 2016 21:58:38 +0200 Subject: [PATCH 1/3] Allow loading specific columns while eagerloading --- src/Illuminate/Database/Eloquent/Builder.php | 14 +++++++++++--- tests/Database/DatabaseEloquentModelTest.php | 11 +++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 3fdb63488705..dfe449d6b818 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -1125,9 +1125,17 @@ protected function parseWithRelations(array $relations) // constraints have been specified for the eager load and we'll just put // an empty Closure with the loader so that we can treat all the same. if (is_numeric($name)) { - $f = function () { - // - }; + if (Str::contains($constraints, ':')) { + list($constraints, $columns) = explode(':', $constraints); + + $f = function ($q) use ($columns) { + $q->select(explode(',', $columns)); + }; + } else { + $f = function () { + // + }; + } list($name, $constraints) = [$constraints, $f]; } diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index ea66b3077e73..afdbb466f6a2 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -147,6 +147,17 @@ public function testWithoutMethodRemovesEagerLoadedRelationshipCorrectly() $this->assertEmpty($instance->getEagerLoads()); } + public function testEager() + { + $model = new EloquentModelWithoutRelationStub; + $instance = $model->newInstance()->newQuery()->with('foo:bar,baz', 'hadi'); + $builder = m::mock(Builder::class); + $builder->shouldReceive('select')->once()->with(['bar', 'baz']); + $this->assertNotNull($instance->getEagerLoads()['hadi']); + $this->assertNotNull($instance->getEagerLoads()['foo']); + $instance->getEagerLoads()['foo']($builder); + } + public function testWithMethodCallsQueryBuilderCorrectlyWithArray() { $result = EloquentModelWithStub::with(['foo', 'bar']); From b314a6965decebf2e50465fb024dff6e3b68ade6 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Tue, 8 Nov 2016 22:04:32 +0200 Subject: [PATCH 2/3] update test name --- tests/Database/DatabaseEloquentModelTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index afdbb466f6a2..a7412f74812a 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -147,7 +147,7 @@ public function testWithoutMethodRemovesEagerLoadedRelationshipCorrectly() $this->assertEmpty($instance->getEagerLoads()); } - public function testEager() + public function testEagerLoadingWithColumns() { $model = new EloquentModelWithoutRelationStub; $instance = $model->newInstance()->newQuery()->with('foo:bar,baz', 'hadi'); From d3e8543273b21b5a9f6e1e20e007e2aab4b9382d Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Tue, 8 Nov 2016 22:27:40 +0200 Subject: [PATCH 3/3] fix test --- tests/Database/DatabaseEloquentModelTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index a7412f74812a..8232aff27af6 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -155,7 +155,8 @@ public function testEagerLoadingWithColumns() $builder->shouldReceive('select')->once()->with(['bar', 'baz']); $this->assertNotNull($instance->getEagerLoads()['hadi']); $this->assertNotNull($instance->getEagerLoads()['foo']); - $instance->getEagerLoads()['foo']($builder); + $closure = $instance->getEagerLoads()['foo']; + $closure($builder); } public function testWithMethodCallsQueryBuilderCorrectlyWithArray()