Skip to content

Commit

Permalink
test(laravel): standard put model should update when it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Sep 20, 2024
1 parent 72a0b66 commit a76aca1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Laravel/Eloquent/State/PersistProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Laravel\Eloquent\State;

use ApiPlatform\Laravel\Eloquent\Metadata\ModelMetadata;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -41,6 +42,15 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
}
}

if (($previousData = $context['previous_data'] ?? null) && $operation instanceof HttpOperation && 'PUT' === $operation->getMethod() && ($operation->getExtraProperties()['standard_put'] ?? true)) {
foreach ($this->modelMetadata->getAttributes($data) as $attribute) {
if ($attribute['primary'] ?? false) {
$data->{$attribute['name']} = $previousData->{$attribute['name']};
}
}
$data->exists = true;
}

$data->saveOrFail();
$data->refresh();

Expand Down
1 change: 0 additions & 1 deletion src/Laravel/Tests/JsonLdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public function testUpdateBook(): void
);
$response->assertStatus(200);
$response->assertJsonFragment([
'@id' => $iri,
'name' => 'updated title',
]);
}
Expand Down

0 comments on commit a76aca1

Please sign in to comment.