Customizing data before saving

On my BD I have:
$table->boolean('is_ready')->default(false);
$table->dateTime('marked_ready_at')->nullable();
$table->boolean('is_ready')->default(false);
$table->dateTime('marked_ready_at')->nullable();
On my form, I have:
Forms\Components\Toggle::make('is_ready'),
Forms\Components\Toggle::make('is_ready'),
In my model, I have:
protected $casts = [
'marked_ready_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
protected $casts = [
'marked_ready_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
I want when the toggle is used the 'marked_ready_at' to automatically grab the current date/time. I have tried the following: On my Pages/Edit
protected function mutateFormDataBeforeSave(array $data): array
{
if ($this->record->is_ready) {
$data['marked_ready_at'] = now();
}

return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
if ($this->record->is_ready) {
$data['marked_ready_at'] = now();
}

return $data;
}
What I/m doing wrong? Is there a better approach? Thanks.
6 Replies
petrisorcraciun
petrisorcraciun16mo ago
You try “afterStateUpdated” for Toggle ? Hidden::make(“marked_ready_at”), Toggle::make(“is_ready”)->afterStateUpdated(function($set, $state) :void {if($state}{$set(“marked_ready_at, now())});
Pablo Torres
Pablo TorresOP16mo ago
and Hi, thank you for your time... I have done:
Forms\Components\Toggle::make('is_ready')
->reactive()
->afterStateUpdated(function (Closure $set, $state) {
$set('marked_ready_at', now());
}),
Forms\Components\DateTimePicker::make('marked_ready_at'),
Forms\Components\Toggle::make('is_ready')
->reactive()
->afterStateUpdated(function (Closure $set, $state) {
$set('marked_ready_at', now());
}),
Forms\Components\DateTimePicker::make('marked_ready_at'),
Works, but... I want the date time to be grabbed when the user saves the record. Like this, the time and date recorded is the one when the toggle was used.
petrisorcraciun
petrisorcraciun16mo ago
You can add an observer for model And for created event set marked_ready_at
Pablo Torres
Pablo TorresOP16mo ago
Thanks... I'll look at that, just one question if you have the time, please. The "mutateFormDataBeforeSave" approach you consider not good for the case? Thanks. https://filamentphp.com/docs/2.x/admin/resources/editing-records#customizing-data-before-saving
Filament
Editing records - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
petrisorcraciun
petrisorcraciun16mo ago
If in method you change condition for if, ex: if($data[‘is_ready’] ?? false) {your code} not working ?
Pablo Torres
Pablo TorresOP16mo ago
Thank you... I think I had my code wrong...
protected function mutateFormDataBeforeSave(array $data): array
{
if (!$this->record->is_ready) {
$data['marked_ready_at'] = now();
}

return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
if (!$this->record->is_ready) {
$data['marked_ready_at'] = now();
}

return $data;
}
By default "is_ready" is false. So, I have to check if it is false: "if ( ! $this->record->is_ready) ..." not: "if ( $this->record->is_ready) ..." Thanks for your time.
Want results from more Discord servers?
Add your server