how to create 1 data to 3 table?

- i have 3 table, expenses, expenses items and expenses payment. expenses : - invoice_number, - date, - status, - total, - remaining_amount expense_items : - expense_id - description - qty - price expense_payments : - expense_id, - date - nominal in this case, when i create expenses data i have payment amount, so i want create expense_payment data, how to create expense_payments data?
9 Replies
Julien B. (aka yebor974)
Hi. Have you relations between these models ? You can use Form layout like grid with relationship
Vicky
VickyOP2w ago
yes i have, but i want create expense_payments is automatic when saving the data. like, when the total expense is $30 and i pay $30 and i create expense_payment automatic
Julien B. (aka yebor974)
if you are in resource you can use afterCreate method. Otherwise use Laravel event/listener. They are manys way to do this, depending on your business process
Vicky
VickyOP2w ago
this the expense table : invoice_number, date, status, total, remaining_amount and this expense payment table : expense_id, date nominal and when create the data, the form send date, total, payment amount, so i want the payment amount field is nominal in expense payment table, and invoice number is auto generate but on this case, when i submit the form, i get error payment amount is not field in expense table, so how to solve it?
Vicky
VickyOP2w ago
this the error
No description
Vicky
VickyOP2w ago
protected function mutateFormDataBeforeCreate(array $data): array { $data['invoice_number'] = 'INV/' . date('ymd') . '/' . str_pad(ExpenseResource::query()->count() + 1, 4, '0', STR_PAD_LEFT); $data['remaining_amount'] = $data['total_price']; if ($data['payment_amount'] == $data['total_price']) { $data['status'] = 'Paid'; } else { $data['status'] = 'Not Paid'; } return $data; } when i use mutate, i get this error
No description
Mohamed Ayaou
Mohamed Ayaou2w ago
mutateFromDataBeforeCreation() is not suitable when you need to assign IDs, try handleRecordCreation() which gives you more control: https://filamentphp.com/docs/3.x/panels/resources/creating-records#customizing-the-creation-process
Vicky
VickyOP2w ago
thanks sir, u save me. and 1 question, why after save the data i and return to edit view the total price, subtotal and payment amount is 0? what can i do for show the value after saving sir?

Did you find this page helpful?