Skip to content

Commit 8bea688

Browse files
[10.x] Add WithoutRelations attribute for model serialization (#47989)
* Add WithoutRelations attribute for model serialization * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent aef975c commit 8bea688

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Illuminate\Queue\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_PROPERTY)]
8+
class WithoutRelations
9+
{
10+
//
11+
}

src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ trait SerializesAndRestoresModelIdentifiers
1515
* Get the property value prepared for serialization.
1616
*
1717
* @param mixed $value
18+
* @param bool $withRelations
1819
* @return mixed
1920
*/
20-
protected function getSerializedPropertyValue($value)
21+
protected function getSerializedPropertyValue($value, $withRelations = true)
2122
{
2223
if ($value instanceof QueueableCollection) {
2324
return (new ModelIdentifier(
2425
$value->getQueueableClass(),
2526
$value->getQueueableIds(),
26-
$value->getQueueableRelations(),
27+
$withRelations ? $value->getQueueableRelations() : [],
2728
$value->getQueueableConnection()
2829
))->useCollectionClass(
2930
($collectionClass = get_class($value)) !== EloquentCollection::class
@@ -36,7 +37,7 @@ protected function getSerializedPropertyValue($value)
3637
return new ModelIdentifier(
3738
get_class($value),
3839
$value->getQueueableId(),
39-
$value->getQueueableRelations(),
40+
$withRelations ? $value->getQueueableRelations() : [],
4041
$value->getQueueableConnection()
4142
);
4243
}

src/Illuminate/Queue/SerializesModels.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Queue;
44

5+
use Illuminate\Queue\Attributes\WithoutRelations;
56
use ReflectionClass;
67
use ReflectionProperty;
78

@@ -45,7 +46,10 @@ public function __serialize()
4546
$name = "\0*\0{$name}";
4647
}
4748

48-
$values[$name] = $this->getSerializedPropertyValue($value);
49+
$values[$name] = $this->getSerializedPropertyValue(
50+
$value,
51+
empty($property->getAttributes(WithoutRelations::class))
52+
);
4953
}
5054

5155
return $values;

tests/Integration/Queue/ModelSerializationTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\Pivot;
88
use Illuminate\Database\Schema\Blueprint;
9+
use Illuminate\Queue\Attributes\WithoutRelations;
910
use Illuminate\Queue\SerializesModels;
1011
use LogicException;
1112
use Orchestra\Testbench\TestCase;
@@ -318,6 +319,19 @@ public function test_model_serialization_structure()
318319
);
319320
}
320321

322+
public function test_it_respects_without_relations_attribute()
323+
{
324+
$user = User::create([
325+
'email' => '[email protected]',
326+
])->load(['roles']);
327+
328+
$serialized = serialize(new ModelSerializationWithoutRelations($user));
329+
330+
$this->assertSame(
331+
'O:69:"Illuminate\Tests\Integration\Queue\ModelSerializationWithoutRelations":1:{s:4:"user";O:45:"Illuminate\Contracts\Database\ModelIdentifier":5:{s:5:"class";s:39:"Illuminate\Tests\Integration\Queue\User";s:2:"id";i:1;s:9:"relations";a:0:{}s:10:"connection";s:7:"testing";s:15:"collectionClass";N;}}', $serialized
332+
);
333+
}
334+
321335
public function test_serialization_types_empty_custom_eloquent_collection()
322336
{
323337
$class = new ModelSerializationTypedCustomCollectionTestClass(
@@ -499,6 +513,19 @@ class ModelSerializationParentAccessibleTestClass extends ModelSerializationAcce
499513
//
500514
}
501515

516+
class ModelSerializationWithoutRelations
517+
{
518+
use SerializesModels;
519+
520+
#[WithoutRelations]
521+
public User $user;
522+
523+
public function __construct(User $user)
524+
{
525+
$this->user = $user;
526+
}
527+
}
528+
502529
class ModelRelationSerializationTestClass
503530
{
504531
use SerializesModels;

0 commit comments

Comments
 (0)