use ->first() instead of ->get()
Method Illuminate\Database\Eloquent\Collection::update does not exist
i want to update an array of data in the table.the structure of my table looks like this employee_id,scheme_id,and the payments_amounts made.i want to update the scheme id and the payment amounts.for each employee.i have tried this code below in the controller.
$updatedschemeids=$savedata['savingschemes_id'];
$updatedamounts=$savedata['update_savingschemes_amount'];
foreach($updatedamounts as $key=>$savingschemefund)
{
$employeecurrentsavings=Employeesavingscheme::where('employee_id',$request->employeeid)->get();
$employeecurrentsavings->update([
$employeecurrentsavings[$key]['savingscheme_id']=$updatedschemeids[$key],
$employeecurrentsavings[$key]['saving_scheme_amount']=$updatedamounts[$key]
]);
}
both the $updateschemeids and $updateamounts are in form of an array respectively.
$updateschemeids array
array:2 [ 0 => "3" 1 => "2" ]
$updateamounts array
array:2 [ 0 => "44500" 1 => "50000"
]
so here what am trying to achieve is to update every scheme ID with its own amount. For example if in the employeesavingschemes table its value is 3 it gets updated with a value of 44500. upon trying that am getting the error Method Illuminate\Database\Eloquent\Collection::update does not exist. where might i have missed the point?
@stephen waweru if you still want to get result with this issue.
see this code as reference
$updatedamounts=[4=>44500, 9=>50000]; // not need to make another array like scemeid, it already do in this array as key 4 and 9 or another key as you want for scheme_id
foreach($updatedamounts as $key => $ammount) {
$employeecurrentsavings=Employeesavingscheme::where('employee_id',$request->employeeid)->where('scheme_id',$key); // add another unique parameter, so it will not update all row based on one parameter only
$employeecurrentsavings->update([
// not need to update the scheme id here, just update the ammount, because the scheme id is already have value from the info you provided before. the shceme id will become another unique key beside employee_id for update logic in table.
'payments_amounts' => $ammount,
]);
}
Please or to participate in this conversation.