Calculating Total Price from Repeater Data
Hey, everyone! I'm currently tackling a challenge with filament forms involving
orders
and products
tables.
Here's the scoop: I'm using a repeater to select multiple products
when creating an order
, and the data gets saved in an order_items
table. My Order
model has a method items()
for the relationship, all good so far.
Now, the tricky part is calculating a total_price
for the order
based on the sum of unit_price
for all selected products
multiplied by their respective quantities. I want to save this total_price
in the orders
table.
Although order_items
are saving correctly, the total_price
isn't making it to the database when saving the order to the orders
table. Any tips on handling this or integrating repeater data with the relationship for accurate calculations? Thanks!11 Replies
so you have problem with showing calculated
total_price
or it is just not saving to the db?Saving to the db.
Any errors? Have to added field to fillable?
No errors while saving data to the db. I can't figure out how to calculate the total_price from the products added on the repeater. Coz I have tried using
$get()
to access parent field (total_price
) from the repeater but still no success.Thanks. I already solved the issue by defining mutateFormDataBeforeCreate() and mutateFormDataBeforeSave() methods for the creating and editing the record respectively as shown in the codes above.
But you aren't showing price live in the forms this way
I tried using the
->afterStateUpdated()
but still can't set the 'total_price'. What could i be doing wrong?i guess you need something like this https://filamentexamples.com/project/invoice-editor-items
did you find a way to do it live?
here is the solution -> https://www.youtube.com/watch?v=6bhF70zZsOM
Filament Daily
YouTube
Filament: Repeater "Live" Form Changes with afterStateUpdated()
Example of Invoice items and calculating the total, with Repeater field and a custom function.
Original tutorial: https://laraveldaily.com/post/filament-repeater-live-calculations-on-update
Full project: https://filamentexamples.com/project/invoice-editor-items