format minutes as hours in form, and save back to database as minutes.

Hi guys, - im not sure what im doing wrong. I store planned_time in minutes in the db. I want the filament form to show in hours, and save back to the db in minutes. Here is my current code. I can either get it to save, or to show on the form properly, but not both. what am I doing wrong? Forms\Components\TextInput::make('planned_time_input') ->label('Planned Time (Hours)') ->numeric() ->minValue(0.25) ->step(0.25) ->helperText('Enter time in hours (e.g., 1.5 for 90 minutes)') ->displayUsing(fn ($value) => number_format($value / 60, 2)) ->afterStateUpdated(fn ($state, Forms\Set $set) => $set('planned_time', $state * 60) ), Forms\Components\Hidden::make('planned_time'),
3 Replies
peteswan23
peteswan23OP2mo ago
Thanks mate! Tried this, but still wont appear in my form.Tried this. Forms\Components\TextInput::make('planned_time') ->dehydrateStateUsing(fn ($state) => number_format($state / 60, 2)), but it doesn't change the formatting.
Blackpig
Blackpig2mo ago
Use the ->dehydrateStateUsing to format the value for saving to the DB, something like ->dehydrateStateUsing(fn (string $state): int => (int)$state * 60)

Did you find this page helpful?