diff --git a/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php b/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php index c731eb473f7f..e6069744dc71 100644 --- a/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php +++ b/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php @@ -80,7 +80,9 @@ protected function restoreCollection($value) $collection = $collection->keyBy->getKey(); - return new EloquentCollection( + $collectionClass = get_class($collection); + + 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'];