Skip to content

Commit 84b9679

Browse files
authored
fix(laravel): persist morph relations (#7170)
1 parent 4bdfd60 commit 84b9679

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/Laravel/Eloquent/State/PersistProcessor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use ApiPlatform\State\ProcessorInterface;
2121
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2222
use Illuminate\Database\Eloquent\Relations\HasMany;
23+
use Illuminate\Database\Eloquent\Relations\MorphMany;
24+
use Illuminate\Database\Eloquent\Relations\MorphTo;
2325

2426
/**
2527
* @implements ProcessorInterface<\Illuminate\Database\Eloquent\Model, \Illuminate\Database\Eloquent\Model>
@@ -45,7 +47,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
4547
continue;
4648
}
4749

48-
if (BelongsTo::class === $relation['type']) {
50+
if (BelongsTo::class === $relation['type'] || MorphTo::class === $relation['type']) {
4951
$rel = $data->{$relation['name']};
5052

5153
if (!$rel->exists) {
@@ -57,7 +59,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
5759
$this->relations[$relation['method_name']] = $relation['name'];
5860
}
5961

60-
if (HasMany::class === $relation['type']) {
62+
if (HasMany::class === $relation['type'] || MorphMany::class === $relation['type']) {
6163
$rel = $data->{$relation['name']};
6264

6365
if (!\is_array($rel)) {

0 commit comments

Comments
 (0)