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
Hi. Have you relations between these models ? You can use Form layout like grid with relationship
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
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
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?
this the error
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
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-processthanks 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?