我需要存储interest_amount
使用get_Attribute
函数计算的内容,但是现在我需要将其插入表中。
我已经使用static::saving
函数来计算interest_amount
我不知道如何$data
在控制器中使用它来调用它
贷款.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;
});
}
}
贷款控制器.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');
}
我最初没有interest_amount
专栏,但后来我添加了它。我不知道如何saving
使用我在控制器中使用的方法调用该函数。