F
Filament15mo ago
RT2023

sum two fields

I have a case where I need to add two field values from a form. The field values belong to the same table. I am using a placeholder to add the two fields and it works. The filed values are named cash and online. the resulting sum is called sales. The code is shown in the attached image. I want to be able to insert the calculated sales value into the database field. I tried the afterStateUpdated method but cant figure out how to sum the values I want. Thanks for your help
No description
No description
2 Replies
cheesegrits
cheesegrits15mo ago
Well, you've made 'sales' a placeholder, which won't get written to the database. In general, regardless of whether / how you display totals like this on the form, it's always best to re-do the math on form submit, to prevent people from spoofing thee total. If you are using forms standalone on your own pages, just do it in your save/update method (after you getData()).
$data = $this->form->getData();
$data['sales'] = $data['cash'] + $data['online'];
$data = $this->form->getData();
$data['sales'] = $data['cash'] + $data['online'];
If you are in an admin panel, do it in a mutateDataBeforeCreate/Save ... https://filamentphp.com/docs/3.x/panels/resources/creating-records#customizing-data-before-saving
RT2023
RT2023OP15mo ago
Thanks Hugh, this is very helpful
Want results from more Discord servers?
Add your server