Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,10 @@ public function setRelations(array $relations)
{
$this->relations = $relations;

foreach ($this->relations as $key => $value) {
$this->propagateRelationAutoloadCallbackToRelation($key, $value);
}

return $this;
}

Expand Down
40 changes: 40 additions & 0 deletions tests/Integration/Database/EloquentModelRelationAutoloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,46 @@ public function testRelationAutoloadWithCircularRelations()
DB::disableQueryLog();
}

public function testRelationAutoloadPropagatesWhenUsingSetRelations()
{
$post1 = Post::create();
$comment1 = $post1->comments()->create(['parent_id' => null]);
$comment1->likes()->create();
$comment1->likes()->create();

$post2 = Post::create();
$comment2 = $post2->comments()->create(['parent_id' => null]);
$comment2->likes()->create();

$posts = Post::get();
$commentsByPost = Comment::get()->groupBy('commentable_id');

$posts->withRelationshipAutoloading();

foreach ($posts as $post) {
$post->setRelations([
'comments' => $commentsByPost->get($post->id, $post->newCollection()),
]);
}

DB::enableQueryLog();

$likes = [];

foreach ($posts as $post) {
foreach ($post->comments as $comment) {
$likes = array_merge($likes, $comment->likes->all());
}
}

$this->assertCount(1, DB::getQueryLog());
$this->assertCount(3, $likes);
$this->assertTrue($posts[0]->comments[0]->relationLoaded('likes'));
$this->assertTrue($posts[1]->comments[0]->relationLoaded('likes'));

DB::disableQueryLog();
}

public function testRelationAutoloadWithChaperoneRelations()
{
Model::automaticallyEagerLoadRelationships();
Expand Down
Loading