Skip to content

Commit f70f69b

Browse files
authored
[12.x] Fix Closure serialization error in automatic relation loading (#55345)
* Fix Serialization of 'Closure' is not allowed error in automatic relation loading * Fix tests
1 parent c4bc126 commit f70f69b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,6 +2501,7 @@ public function __sleep()
25012501

25022502
$this->classCastCache = [];
25032503
$this->attributeCastCache = [];
2504+
$this->relationAutoloadCallback = null;
25042505

25052506
return array_keys(get_object_vars($this));
25062507
}
@@ -2515,5 +2516,9 @@ public function __wakeup()
25152516
$this->bootIfNotBooted();
25162517

25172518
$this->initializeTraits();
2519+
2520+
if (static::isAutomaticallyEagerLoadingRelationships()) {
2521+
$this->withRelationshipAutoloading();
2522+
}
25182523
}
25192524
}

tests/Integration/Database/EloquentModelRelationAutoloadTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@ public function testRelationAutoloadForSingleModel()
8686
$this->assertTrue($post->comments[0]->relationLoaded('likes'));
8787
}
8888

89+
public function testRelationAutoloadWithSerialization()
90+
{
91+
Model::automaticallyEagerLoadRelationships();
92+
93+
$post = Post::create();
94+
$comment1 = $post->comments()->create(['parent_id' => null]);
95+
$comment2 = $post->comments()->create(['parent_id' => $comment1->id]);
96+
$comment2->likes()->create();
97+
98+
DB::enableQueryLog();
99+
100+
$likes = [];
101+
102+
$post = serialize($post);
103+
$post = unserialize($post);
104+
105+
foreach ($post->comments as $comment) {
106+
$likes = array_merge($likes, $comment->likes->all());
107+
}
108+
109+
$this->assertCount(2, DB::getQueryLog());
110+
111+
Model::automaticallyEagerLoadRelationships(false);
112+
}
113+
89114
public function testRelationAutoloadVariousNestedMorphRelations()
90115
{
91116
tap(Post::create(), function ($post) {

0 commit comments

Comments
 (0)