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; diff --git a/tests/Integration/Queue/ModelSerializationTest.php b/tests/Integration/Queue/ModelSerializationTest.php index d3bf33c642c1..421e9b764537 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;