Skip to content

Commit

Permalink
Fix chained eager loading (#13967)
Browse files Browse the repository at this point in the history
  • Loading branch information
acasar authored and taylorotwell committed Jun 13, 2016
1 parent 7a4a022 commit 14c808d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
36 changes: 20 additions & 16 deletions tests/Database/DatabaseEloquentPolymorphicIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
}

/**
Expand Down Expand Up @@ -174,6 +177,7 @@ class TestComment extends Eloquent
{
protected $table = 'comments';
protected $guarded = [];
protected $with = ['commentable'];

public function owner()
{
Expand Down

0 comments on commit 14c808d

Please sign in to comment.