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..8232aff27af6 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -147,6 +147,18 @@ public function testWithoutMethodRemovesEagerLoadedRelationshipCorrectly() $this->assertEmpty($instance->getEagerLoads()); } + public function testEagerLoadingWithColumns() + { + $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']); + $closure = $instance->getEagerLoads()['foo']; + $closure($builder); + } + public function testWithMethodCallsQueryBuilderCorrectlyWithArray() { $result = EloquentModelWithStub::with(['foo', 'bar']);