From 4525df0997f418fe6dad386296636b08a531ca40 Mon Sep 17 00:00:00 2001 From: Timothy Winters Date: Tue, 16 Jul 2019 17:34:49 -0400 Subject: [PATCH 1/2] Instantiate restored collection class --- .../SerializesAndRestoresModelIdentifiers.php | 3 +- .../Queue/ModelSerializationTest.php | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php b/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php index c731eb473f7f..46da666a1aee 100644 --- a/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php +++ b/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php @@ -79,8 +79,9 @@ protected function restoreCollection($value) } $collection = $collection->keyBy->getKey(); + $collectionClass = get_class($collection); - return new EloquentCollection( + return new $collectionClass( collect($value->id)->map(function ($id) use ($collection) { return $collection[$id]; }) diff --git a/tests/Integration/Queue/ModelSerializationTest.php b/tests/Integration/Queue/ModelSerializationTest.php index c56eb5f8c42f..4dd864178ac3 100644 --- a/tests/Integration/Queue/ModelSerializationTest.php +++ b/tests/Integration/Queue/ModelSerializationTest.php @@ -235,6 +235,20 @@ public function test_it_serializes_a_collection_in_correct_order() $this->assertEquals($unserialized->users->first()->email, 'taylor@laravel.com'); $this->assertEquals($unserialized->users->last()->email, 'mohamed@laravel.com'); } + + public function test_it_can_unserialize_custom_collection() + { + ModelSerializationTestCustomUser::create(['email' => 'mohamed@laravel.com']); + ModelSerializationTestCustomUser::create(['email' => 'taylor@laravel.com']); + + $serialized = serialize(new CollectionSerializationTestClass( + ModelSerializationTestCustomUser::all() + )); + + $unserialized = unserialize($serialized); + + $this->assertInstanceOf(ModelSerializationTestCustomUserCollection::class, $unserialized->users); + } } class ModelSerializationTestUser extends Model @@ -244,6 +258,22 @@ class ModelSerializationTestUser extends Model public $timestamps = false; } +class ModelSerializationTestCustomUserCollection extends Collection +{ +} + +class ModelSerializationTestCustomUser extends Model +{ + public $table = 'users'; + public $guarded = ['id']; + public $timestamps = false; + + public function newCollection(array $models = []) + { + return new ModelSerializationTestCustomUserCollection($models); + } +} + class Order extends Model { public $guarded = ['id']; From 17b03a82f7eb6daaa9cba4fab7a12fb8132ca2fc Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 17 Jul 2019 08:02:39 -0500 Subject: [PATCH 2/2] Update SerializesAndRestoresModelIdentifiers.php --- src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php b/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php index 46da666a1aee..e6069744dc71 100644 --- a/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php +++ b/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php @@ -79,6 +79,7 @@ protected function restoreCollection($value) } $collection = $collection->keyBy->getKey(); + $collectionClass = get_class($collection); return new $collectionClass(