Skip to content

Commit a76aca1

Browse files
committed
test(laravel): standard put model should update when it exists
1 parent 72a0b66 commit a76aca1

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Laravel/Eloquent/State/PersistProcessor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Laravel\Eloquent\State;
1515

1616
use ApiPlatform\Laravel\Eloquent\Metadata\ModelMetadata;
17+
use ApiPlatform\Metadata\HttpOperation;
1718
use ApiPlatform\Metadata\Operation;
1819
use ApiPlatform\State\ProcessorInterface;
1920
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -41,6 +42,15 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
4142
}
4243
}
4344

45+
if (($previousData = $context['previous_data'] ?? null) && $operation instanceof HttpOperation && 'PUT' === $operation->getMethod() && ($operation->getExtraProperties()['standard_put'] ?? true)) {
46+
foreach ($this->modelMetadata->getAttributes($data) as $attribute) {
47+
if ($attribute['primary'] ?? false) {
48+
$data->{$attribute['name']} = $previousData->{$attribute['name']};
49+
}
50+
}
51+
$data->exists = true;
52+
}
53+
4454
$data->saveOrFail();
4555
$data->refresh();
4656

src/Laravel/Tests/JsonLdTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public function testUpdateBook(): void
9797
);
9898
$response->assertStatus(200);
9999
$response->assertJsonFragment([
100-
'@id' => $iri,
101100
'name' => 'updated title',
102101
]);
103102
}

0 commit comments

Comments
 (0)