Insert to one table from another table

Hi, in my filament project i have two tables 'Loan' and 'Payment'. When i insert data to Loan table using LoanResource Form I also want to send some of the data to Payment table. I tried using afterCreate() life cycle hook, but i could not pass data to it. Is there any other way, like custom controller.
10 Replies
Patrick Boivin
Can you explain what you are trying to do? What does it mean to send data from the Load table to the Payment table?
Bloom
BloomOP2y ago
i have columns like loan_amount, loan_date on Loan Table and i have the same column on Payment Table as well. I want to send those same data as soon as Loan is created because i want that data to be as the initial balance for Payment.
Patrick Boivin
Does the payment get created automatically when you create a loan?
Bloom
BloomOP2y ago
protected function mutateFormDataBeforeCreate(array $data): array
{
$payment_data = new payment;
$payment_data->debit = $data['loan_amount'];
$payment_data->save();
protected function mutateFormDataBeforeCreate(array $data): array
{
$payment_data = new payment;
$payment_data->debit = $data['loan_amount'];
$payment_data->save();
i have the above code in mutateFormDataBeforeCreate but i could not create payment because the payment is referencing to loan i tried using afterCreate() but i cannot pass the loan data which is just created
Patrick Boivin
Right, I think afterCreate() is probably what you need... can you show me what you have tried?
Bloom
BloomOP2y ago
protected function afterCreate(array $data): array
{
$loan_id = $data['id'];
$loan_date=loan::where('loan_id', $loan_id)->pluck('loan_date')->first();
$payment = new payment;
$payment->loan_date = $loan_date;
//
$payment->save();
protected function afterCreate(array $data): array
{
$loan_id = $data['id'];
$loan_date=loan::where('loan_id', $loan_id)->pluck('loan_date')->first();
$payment = new payment;
$payment->loan_date = $loan_date;
//
$payment->save();
i tried to pass the value to afterCreate but i am getting error too few arguments
Patrick Boivin
afterCreate() doesn't receive any parameter... you can read it from $this->data['id'] I think
Bloom
BloomOP2y ago
ok let me try i still get Integrity Constraint violation error, since loan_id on Payment is referencing to loan_id on Loan Table.
Patrick Boivin
$this->record->id
Bloom
BloomOP2y ago
my mistake, it works now. Thank you so much, i have been trying for the whole day. Thank you alot.
Want results from more Discord servers?
Add your server