F
Filament5mo ago
Raven

What's a better way to handle this?

TextInput::make('quantity')->numeric()->required()->live(onBlur: true)
->afterStateUpdated(fn(Set $set, Get $get, ?int $state) => $set('total_cost',
$state * $get('quantity') * (1 + $get('tax') / 100))),
TextInput::make('tax')->numeric()->suffix('%')->required()->live(onBlur: true)
->afterStateUpdated(fn(Set $set, Get $get, ?int $state) => $set('total_cost',
$state * $get('quantity') * (1 + $get('tax') / 100))),
TextInput::make('unit_cost')->numeric()->suffix('USD')->required()->live(onBlur: true)
->afterStateUpdated(fn(Set $set, Get $get, ?int $state) => $set('total_cost',
$state * $get('quantity') * (1 + $get('tax') / 100))),
TextInput::make('total_cost')->numeric()->suffix('USD')->required()->disabled(),
TextInput::make('quantity')->numeric()->required()->live(onBlur: true)
->afterStateUpdated(fn(Set $set, Get $get, ?int $state) => $set('total_cost',
$state * $get('quantity') * (1 + $get('tax') / 100))),
TextInput::make('tax')->numeric()->suffix('%')->required()->live(onBlur: true)
->afterStateUpdated(fn(Set $set, Get $get, ?int $state) => $set('total_cost',
$state * $get('quantity') * (1 + $get('tax') / 100))),
TextInput::make('unit_cost')->numeric()->suffix('USD')->required()->live(onBlur: true)
->afterStateUpdated(fn(Set $set, Get $get, ?int $state) => $set('total_cost',
$state * $get('quantity') * (1 + $get('tax') / 100))),
TextInput::make('total_cost')->numeric()->suffix('USD')->required()->disabled(),
Solution:
In our demo project we created a separate function in the resource and called it from whatever component that changed value. Difficult to copy paste from my phone so providing the screenshot.
No description
Jump to solution
3 Replies
Raven
RavenOP5mo ago
I have a total cost field (that's also present in the database), and I want it to be updated with the qty * unit * tax after any change.
Solution
Povilas K
Povilas K5mo ago
In our demo project we created a separate function in the resource and called it from whatever component that changed value. Difficult to copy paste from my phone so providing the screenshot.
No description
Raven
RavenOP5mo ago
Smart, thank you!

Did you find this page helpful?