Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Fix BelongsToMany pivot relationship child with loaded relations wakeup #27358

Merged
merged 3 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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