From 07d4a129ee0a99859ea0445a844f35cac1cf8884 Mon Sep 17 00:00:00 2001 From: Erik Gaal Date: Wed, 30 Jan 2019 11:32:21 +0100 Subject: [PATCH 1/3] Add regression test for serializing relations with pivot relations --- .../Queue/ModelSerializationTest.php | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/Integration/Queue/ModelSerializationTest.php b/tests/Integration/Queue/ModelSerializationTest.php index d3bf33c642c1..149a4c2efaba 100644 --- a/tests/Integration/Queue/ModelSerializationTest.php +++ b/tests/Integration/Queue/ModelSerializationTest.php @@ -7,6 +7,7 @@ use Illuminate\Queue\SerializesModels; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Relations\Pivot; /** * @group integration @@ -59,6 +60,15 @@ public function setUp() Schema::create('products', function ($table) { $table->increments('id'); }); + + Schema::create('roles', function ($table) { + $table->increments('id'); + }); + + Schema::create('role_user', function ($table) { + $table->unsignedInteger('user_id'); + $table->unsignedInteger('role_id'); + }); } public function test_it_serialize_user_on_default_connection() @@ -176,6 +186,31 @@ public function test_it_reloads_nested_relationships() $this->assertEquals($nestedUnSerialized->order->getRelations(), $order->getRelations()); } + /** + * Regression test for https://github.com/laravel/framework/issues/23068 + */ + public function test_it_can_unserialize_nested_relationships_without_pivot() + { + $user = tap(User::create([ + 'email' => 'taylor@laravel.com', + ]), function (User $user) { + $user->wasRecentlyCreated = false; + }); + + $role1 = Role::create(); + $role2 = Role::create(); + + RoleUser::create(['user_id' => $user->id, 'role_id' => $role1->id]); + RoleUser::create(['user_id' => $user->id, 'role_id' => $role2->id]); + + $user->roles->each(function ($role) { + $role->pivot->load('user', 'role'); + }); + + $serialized = serialize(new ModelSerializationTestClass($user)); + unserialize($serialized); + } + public function test_it_serializes_an_empty_collection() { $serialized = serialize(new ModelSerializationTestClass( @@ -231,6 +266,46 @@ class Product extends Model public $timestamps = false; } +class User extends Model +{ + public $guarded = ['id']; + public $timestamps = false; + + public function roles() + { + return $this->belongsToMany(Role::class) + ->using(RoleUser::class); + } +} + +class Role extends Model +{ + public $guarded = ['id']; + public $timestamps = false; + + public function users() + { + return $this->belongsToMany(User::class) + ->using(RoleUser::class); + } +} + +class RoleUser extends Pivot +{ + public $guarded = ['id']; + public $timestamps = false; + + public function user() + { + return $this->belongsTo(User::class); + } + + public function role() + { + return $this->belongsTo(Role::class); + } +} + class ModelSerializationTestClass { use SerializesModels; From 1d7fafc50ec24e4506054abfcb9b384fb2aa7382 Mon Sep 17 00:00:00 2001 From: Erik Gaal Date: Wed, 30 Jan 2019 11:52:19 +0100 Subject: [PATCH 2/3] Add period to end of sentence --- tests/Integration/Queue/ModelSerializationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Queue/ModelSerializationTest.php b/tests/Integration/Queue/ModelSerializationTest.php index 149a4c2efaba..421e9b764537 100644 --- a/tests/Integration/Queue/ModelSerializationTest.php +++ b/tests/Integration/Queue/ModelSerializationTest.php @@ -187,7 +187,7 @@ public function test_it_reloads_nested_relationships() } /** - * Regression test for https://github.com/laravel/framework/issues/23068 + * Regression test for https://github.com/laravel/framework/issues/23068. */ public function test_it_can_unserialize_nested_relationships_without_pivot() { From 88a1600544914ab2b31afbf3b1f5c4831cc8f82c Mon Sep 17 00:00:00 2001 From: Erik Gaal Date: Wed, 30 Jan 2019 11:54:56 +0100 Subject: [PATCH 3/3] Fix wakeup for pivot relationships --- src/Illuminate/Database/Eloquent/Model.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 5d5308a86ead..f6568e734526 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -1408,10 +1408,12 @@ public function getQueueableRelations() $relations = []; foreach ($this->getRelations() as $key => $relation) { - if (method_exists($this, $key)) { - $relations[] = $key; + if (! method_exists($this, $key)) { + continue; } + $relations[] = $key; + if ($relation instanceof QueueableCollection) { foreach ($relation->getQueueableRelations() as $collectionValue) { $relations[] = $key.'.'.$collectionValue;