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
RavenOP5d 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 K4d 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
RavenOP4d ago
Smart, thank you!
Want results from more Discord servers?
Add your server