diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index ebb52abd3e45..cfa7461634d4 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -187,7 +187,7 @@ protected function getResultsByType($type) $query = $this->replayMacros($instance->newQuery()) ->mergeModelDefinedRelationConstraints($this->getQuery()) - ->setEagerLoads($eagerLoads); + ->with($eagerLoads); return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get(); } diff --git a/tests/Database/DatabaseEloquentPolymorphicIntegrationTest.php b/tests/Database/DatabaseEloquentPolymorphicIntegrationTest.php index e970425b48f8..b09cb6a810a5 100644 --- a/tests/Database/DatabaseEloquentPolymorphicIntegrationTest.php +++ b/tests/Database/DatabaseEloquentPolymorphicIntegrationTest.php @@ -74,43 +74,46 @@ public function tearDown() public function testItLoadsRelationshipsAutomatically() { - $this->createUsers(); - - $taylor = TestUser::first(); - $taylor->posts()->create(['title' => 'A title', 'body' => 'A body']) - ->comments()->create(['body' => 'A comment body', 'user_id' => 1]) - ->likes()->create([]); + $this->seedData(); $like = TestLikeWithSingleWith::first(); $this->assertTrue($like->relationLoaded('likeable')); - $this->assertEquals(TestComment::first(), $like->likeable); } - public function testItLoadsNestedRelationshipsAutomatically() + public function testItLoadsChainedRelationshipsAutomatically() { - $this->createUsers(); + $this->seedData(); - $taylor = TestUser::first(); - $taylor->posts()->create(['title' => 'A title', 'body' => 'A body']) - ->comments()->create(['body' => 'A comment body', 'user_id' => 1]) - ->likes()->create([]); + $like = TestLikeWithSingleWith::first(); + + $this->assertTrue($like->likeable->relationLoaded('commentable')); + $this->assertEquals(TestPost::first(), $like->likeable->commentable); + } + + public function testItLoadsNestedRelationshipsAutomatically() + { + $this->seedData(); $like = TestLikeWithNestedWith::first(); $this->assertTrue($like->relationLoaded('likeable')); $this->assertTrue($like->likeable->relationLoaded('owner')); - $this->assertEquals($taylor, $like->likeable->owner); + $this->assertEquals(TestUser::first(), $like->likeable->owner); } /** * Helpers... */ - protected function createUsers() + protected function seedData() { - TestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']); + $taylor = TestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']); + + $taylor->posts()->create(['title' => 'A title', 'body' => 'A body']) + ->comments()->create(['body' => 'A comment body', 'user_id' => 1]) + ->likes()->create([]); } /** @@ -174,6 +177,7 @@ class TestComment extends Eloquent { protected $table = 'comments'; protected $guarded = []; + protected $with = ['commentable']; public function owner() {