0

I need to store interest_amount which is computed using get_Attribute function however now I need to insert it into table.

I have used static::saving function to compute interest_amount I don't know how to call it using $data in controller

Loan.php

use Illuminate\Database\Eloquent\Model;

class Loan extends Model
{
    protected $fillable = [
        'amount',
        'interest',
        'status',
        'duration',
        'member_id',
        'loan_type_id',
        'interest_type_id',
        'loan_payment_type_id'
    ];

    protected $appends = 'interest_amount';

    public function getInterestAmountAttribute()
    {
        return ($this->amount)/100 * $this->interest;
    }

    public function member()
    {
        return $this->belongsTo(Member::class,'member_id','id');
    }

    protected static function boot() 
    {
        parent::boot();

        static::saving(function($model) {
            $model->interest_amount = ($model->amount/100 ) * $model->interest;
        });
    }
}

LoanController.php

$data['amount'] = $request->amount;
$data['interest'] = $request->interest;
$data['interest_amount'] = $interest_amount;
$data['duration'] = $request->duration;
$data['status']= $request->status;
$data['member_id'] = $request->name;
$data['loan_type_id']= $request->loan_type;
$data['interest_type_id']= $request->interest_type;
$data['loan_payment_type_id']= $request->payment;

if(DB::table('loans')->insert($data)){
    return redirect()->route('loan')->with('success','data was successfuly inserted');
}

I initially didn't have interest_amount column but I added it later. I don't know how to call the saving function with the approach I have used in my controller.

Remul
  • 7,874
  • 1
  • 13
  • 30

1 Answers1

1

Model events will not fire when you use the query builder to insert data into your tables.

You have to use the Model to save the data, something like this:

If your model attributes are mass assignable:

if((new Loan($data))->save()){
    return redirect()->route('loan')->with('success','data was successfuly inserted');
}

otherwise:

$loan = new Loan;
$loan->amount = $request->amount;
$loan->interest = $request->interest;
$loan->interest_amount = $interest_amount;
$loan->duration = $request->duration;
$loan->status = $request->status;
$loan->member_id = $request->name;
$loan->loan_type_id = $request->loan_type;
$loan->interest_type_id = $request->interest_type;
$loan->loan_payment_type_id = $request->payment;

if($loan->save()){
    return redirect()->route('loan')->with('success','data was successfuly inserted');
}
Remul
  • 7,874
  • 1
  • 13
  • 30
  • i have used " $data[] " approach on my controller so i'll have to change it all. i have already seen the method above by creating new object. is it possible to use the same approach as mine?? – Prithvi Tuladhar Apr 17 '19 at 10:17
  • okay it is working. the statement '$data['interest_amout'] = $interest_amount;' was initially giving error i removed it and it works. thank you... – Prithvi Tuladhar Apr 17 '19 at 10:36
  • is it possible to insert this computed value into a different child table?? – Prithvi Tuladhar Apr 23 '19 at 06:58