Skip to content

Commit d269f36

Browse files
authored
Merge pull request #27358 from erikgaal/5.7
[5.7] Fix BelongsToMany pivot relationship child with loaded relations wakeup
2 parents a0ba83d + 88a1600 commit d269f36

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,10 +1408,12 @@ public function getQueueableRelations()
14081408
$relations = [];
14091409

14101410
foreach ($this->getRelations() as $key => $relation) {
1411-
if (method_exists($this, $key)) {
1412-
$relations[] = $key;
1411+
if (! method_exists($this, $key)) {
1412+
continue;
14131413
}
14141414

1415+
$relations[] = $key;
1416+
14151417
if ($relation instanceof QueueableCollection) {
14161418
foreach ($relation->getQueueableRelations() as $collectionValue) {
14171419
$relations[] = $key.'.'.$collectionValue;

tests/Integration/Queue/ModelSerializationTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Queue\SerializesModels;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Eloquent\Collection;
10+
use Illuminate\Database\Eloquent\Relations\Pivot;
1011

1112
/**
1213
* @group integration
@@ -59,6 +60,15 @@ public function setUp()
5960
Schema::create('products', function ($table) {
6061
$table->increments('id');
6162
});
63+
64+
Schema::create('roles', function ($table) {
65+
$table->increments('id');
66+
});
67+
68+
Schema::create('role_user', function ($table) {
69+
$table->unsignedInteger('user_id');
70+
$table->unsignedInteger('role_id');
71+
});
6272
}
6373

6474
public function test_it_serialize_user_on_default_connection()
@@ -176,6 +186,31 @@ public function test_it_reloads_nested_relationships()
176186
$this->assertEquals($nestedUnSerialized->order->getRelations(), $order->getRelations());
177187
}
178188

189+
/**
190+
* Regression test for https://github.com/laravel/framework/issues/23068.
191+
*/
192+
public function test_it_can_unserialize_nested_relationships_without_pivot()
193+
{
194+
$user = tap(User::create([
195+
'email' => 'taylor@laravel.com',
196+
]), function (User $user) {
197+
$user->wasRecentlyCreated = false;
198+
});
199+
200+
$role1 = Role::create();
201+
$role2 = Role::create();
202+
203+
RoleUser::create(['user_id' => $user->id, 'role_id' => $role1->id]);
204+
RoleUser::create(['user_id' => $user->id, 'role_id' => $role2->id]);
205+
206+
$user->roles->each(function ($role) {
207+
$role->pivot->load('user', 'role');
208+
});
209+
210+
$serialized = serialize(new ModelSerializationTestClass($user));
211+
unserialize($serialized);
212+
}
213+
179214
public function test_it_serializes_an_empty_collection()
180215
{
181216
$serialized = serialize(new ModelSerializationTestClass(
@@ -231,6 +266,46 @@ class Product extends Model
231266
public $timestamps = false;
232267
}
233268

269+
class User extends Model
270+
{
271+
public $guarded = ['id'];
272+
public $timestamps = false;
273+
274+
public function roles()
275+
{
276+
return $this->belongsToMany(Role::class)
277+
->using(RoleUser::class);
278+
}
279+
}
280+
281+
class Role extends Model
282+
{
283+
public $guarded = ['id'];
284+
public $timestamps = false;
285+
286+
public function users()
287+
{
288+
return $this->belongsToMany(User::class)
289+
->using(RoleUser::class);
290+
}
291+
}
292+
293+
class RoleUser extends Pivot
294+
{
295+
public $guarded = ['id'];
296+
public $timestamps = false;
297+
298+
public function user()
299+
{
300+
return $this->belongsTo(User::class);
301+
}
302+
303+
public function role()
304+
{
305+
return $this->belongsTo(Role::class);
306+
}
307+
}
308+
234309
class ModelSerializationTestClass
235310
{
236311
use SerializesModels;

0 commit comments

Comments
 (0)