Skip to content

Commit bdca755

Browse files
[9.x] Fix updated_at timestamp not updating when field is read-only (#807)
1 parent d5747ec commit bdca755

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

src/Http/Controllers/CP/Traits/PreparesModels.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ protected function prepareModelForSaving(Resource $resource, Model &$model, Requ
182182
return;
183183
}
184184

185+
// Skip read-only timestamp columns so Laravel's automatic
186+
// timestamp handling isn't prevented by a stale value.
187+
if (
188+
$field->visibility() === 'read_only'
189+
&& $model->usesTimestamps()
190+
&& in_array($field->handle(), [$model->getCreatedAtColumn(), $model->getUpdatedAtColumn()])
191+
) {
192+
return;
193+
}
194+
185195
$model->setAttribute($field->handle(), $processedValue);
186196
});
187197
}

tests/Http/Controllers/CP/ResourceControllerTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,47 @@ public function can_update_resource_and_ensure_computed_field_isnt_saved_to_data
678678
}
679679

680680
#[Test]
681-
public function can_update_resource_and_ensure__field_isnt_saved_to_database()
681+
public function can_update_resource_and_ensure_read_only_timestamp_field_doesnt_prevent_timestamp_update()
682+
{
683+
$postBlueprint = Blueprint::find('runway::post');
684+
685+
Blueprint::shouldReceive('find')->with('user')->andReturn(new \Statamic\Fields\Blueprint);
686+
Blueprint::shouldReceive('find')->with('runway::author')->andReturn(new \Statamic\Fields\Blueprint);
687+
Blueprint::shouldReceive('find')->with('runway::post')->andReturn($postBlueprint->ensureField('updated_at', [
688+
'type' => 'date',
689+
'time_enabled' => true,
690+
'visibility' => 'read_only',
691+
]));
692+
Blueprint::shouldReceive('getAdditionalNamespaces')->andReturn(collect(['runway' => base_path('resources/blueprints/runway')]))->zeroOrMoreTimes();
693+
694+
$post = Post::factory()->create();
695+
$user = User::make()->makeSuper()->save();
696+
697+
$originalUpdatedAt = $post->updated_at;
698+
699+
$this->travel(5)->minutes();
700+
701+
$this
702+
->actingAs($user)
703+
->patch(cp_route('runway.update', ['resource' => 'post', 'model' => $post->id]), [
704+
'published' => true,
705+
'title' => 'Santa is coming home',
706+
'slug' => 'santa-is-coming-home',
707+
'body' => $post->body,
708+
'author_id' => [$post->author_id],
709+
'updated_at' => $originalUpdatedAt->toIso8601ZuluString('millisecond'),
710+
])
711+
->assertOk()
712+
->assertJsonStructure(['data', 'saved']);
713+
714+
$post->refresh();
715+
716+
$this->assertEquals('Santa is coming home', $post->title);
717+
$this->assertTrue($post->updated_at->gt($originalUpdatedAt));
718+
}
719+
720+
#[Test]
721+
public function can_update_resource_and_ensure_field_with_save_false_isnt_saved_to_database()
682722
{
683723
$post = Post::factory()->create();
684724
$user = User::make()->makeSuper()->save();

0 commit comments

Comments
 (0)