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!
No description
No description
No description
11 Replies
krekas
krekas12mo ago
so you have problem with showing calculated total_price or it is just not saving to the db?
ianclemence
ianclemenceOP12mo ago
Saving to the db.
krekas
krekas12mo ago
Any errors? Have to added field to fillable?
ianclemence
ianclemenceOP12mo ago
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.
ianclemence
ianclemenceOP12mo ago
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.
No description
No description
krekas
krekas12mo ago
But you aren't showing price live in the forms this way
ianclemence
ianclemenceOP12mo ago
I tried using the ->afterStateUpdated() but still can't set the 'total_price'. What could i be doing wrong?
No description
krekas
krekas12mo ago
ingmontoya
ingmontoya12mo ago
did you find a way to do it live?

Did you find this page helpful?