FilamentF
Filament13mo 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(),
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.
Screenshot_20241130-215207_Chrome.jpg
Was this page helpful?