Skip to content

Commit

Permalink
Merge pull request #27358 from erikgaal/5.7
Browse files Browse the repository at this point in the history
[5.7] Fix BelongsToMany pivot relationship child with loaded relations wakeup
  • Loading branch information
taylorotwell authored Jan 30, 2019
2 parents a0ba83d + 88a1600 commit d269f36
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
75 changes: 75 additions & 0 deletions tests/Integration/Queue/ModelSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d269f36

Please sign in to comment.