|
7 | 7 | use Illuminate\Queue\SerializesModels;
|
8 | 8 | use Illuminate\Database\Eloquent\Model;
|
9 | 9 | use Illuminate\Database\Eloquent\Collection;
|
| 10 | +use Illuminate\Database\Eloquent\Relations\Pivot; |
10 | 11 |
|
11 | 12 | /**
|
12 | 13 | * @group integration
|
@@ -59,6 +60,15 @@ public function setUp()
|
59 | 60 | Schema::create('products', function ($table) {
|
60 | 61 | $table->increments('id');
|
61 | 62 | });
|
| 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 | + }); |
62 | 72 | }
|
63 | 73 |
|
64 | 74 | public function test_it_serialize_user_on_default_connection()
|
@@ -176,6 +186,31 @@ public function test_it_reloads_nested_relationships()
|
176 | 186 | $this->assertEquals($nestedUnSerialized->order->getRelations(), $order->getRelations());
|
177 | 187 | }
|
178 | 188 |
|
| 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 | + |
179 | 214 | public function test_it_serializes_an_empty_collection()
|
180 | 215 | {
|
181 | 216 | $serialized = serialize(new ModelSerializationTestClass(
|
@@ -231,6 +266,46 @@ class Product extends Model
|
231 | 266 | public $timestamps = false;
|
232 | 267 | }
|
233 | 268 |
|
| 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 | + |
234 | 309 | class ModelSerializationTestClass
|
235 | 310 | {
|
236 | 311 | use SerializesModels;
|
|
0 commit comments