I have a function that updates certain fields of my table after performing the different computation,
I don’t want to commit the changes to the database until a click function via a button is done.
[public function totalDeductions()
{
$staff = Staffrecord::with('loans')->where('staff_id', $this->staff_id)->first();
if ($staff->loans->isNotEmpty()) {
$this->loanMonthlyDeductions = [];
foreach ($staff->loans as $loan) {
$this->loanDuration = $loan->duration;
$this->loanAmount = $loan->loan_amount;
foreach ($loan->loantype as $loan_name) {
switch (true) {
case ($this->payrollDate === $this->loanPayStartDate):
$this->loanMonthlyDeductions[] = ['loan_amount' => $this->loanAmount / $this->loanDuration, 'loantype' => $loan_name->name, 'OM' => $this->loanDuration, 'duration' => $loan->duration, 'status' => $loan->status];
break;
case ($this->payrollDate > $this->loanPayStartDate and $this->payrollDate < $this->loanPayEndDate):
$this->loanMonthlyDeductions[] = ['loan_amount' => $this->loanAmount / $this->loanDuration, 'loantype' => $loan_name->name, 'OM' => $this->loanDuration, 'duration' => $loan->duration, 'status' => $loan->status];
break;
case ($this->payrollDate === $this->loanPayEndDate):
$this->loanMonthlyDeductions[] = ['loan_amount' => $this->loanAmount / $this->loanDuration, 'loantype' => $loan_name->name, 'OM' => $this->loanDuration, 'duration' => $loan->duration, 'status' => $loan->status];
break;
default:
break;
}
}
}
$this->loanDeductionSum = collect($this->loanMonthlyDeductions)->sum('loan_amount');
if ($this->loanMonthlyDeductions) {
foreach ($this->loanMonthlyDeductions as $loanMonthlyDeduction) {
if (in_array('active', $loanMonthlyDeduction)) {
}
}]
This is throwing this error Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of
However when I do like so I seem to find errors here is my solution
$staff->loans->update();
$staff->loans()->update([
'status' => 'active',
'loan_amount' => $this->loanAmount - $loanMonthlyDeduction['loan_amount'],
'duration' => $loanMonthlyDeduction['duration'] - 1,
]);
My problem is that I want the update to be called from a button on the live component