Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephen waweru's avatar

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?

0 likes
13 replies
stephen waweru's avatar

@Sergiu17 first only gets the first object from the collection.lets say an employee has many scheme it will only get the first one in the collection.

Sergiu17's avatar

@stephen waweru this should do then

Employeesavingscheme::update([
  $employeecurrentsavings[$key]['savingscheme_id']=$updatedschemeids[$key],
  $employeecurrentsavings[$key]['saving_scheme_amount']=$updatedamounts[$key]
])->where('employee_id',$request->employeeid)
krisi_gjika's avatar

you need to call update on the eloquent builder instance, not on the collection

$query = Employeesavingscheme::query()->where('employee_id',$request->employeeid);

$query->update([
  'scheme_id' => $updatedschemeids[$key],
  'payments_amounts' => $updatedamounts[$key],
]);
krisi_gjika's avatar

@stephen waweru it doesn't work is not a response from a developer. what doesn't work? what did you try?

did you include these keys in the model's fillable array?

stephen waweru's avatar

@krisi_gjika i meant the code you answered above.it gets the last scheme id and the last amount in the request and saves it in all the schemeid and scheme amount columns for the for the employee ids respectively. yes I've included the keys

tangtang's avatar

@stephen waweru the code from @krisi_gjika is the right method to update some data from looping task.

the reason why it gets the last scheme id and the last amount in the request and saves it in all the schemeid and scheme amount columns for the for the employee ids respectively this happen because you update the table based on employee_id

let's assume there table with column id(primary key and auto incre), employee_id , schemeid, schemeid_ammount. and there 3 row with same employee_id. if you do an update data even it not from looping task. it will update all row with same data based on employee_id.

stephen waweru's avatar

@tangtang i have understood but here i have multiple scheme ids each having its own amount .what i want ot achive is let say we have 2 scheme ids schemeid 1 and scheme id 2,both have scheme amount values of 4000 and 5000 in the database.i want to update scheme id 1 with scheme amount 5000 and scheme id 2 with scheme amount 6000.this is what i am trying to achieve

tangtang's avatar

@stephen waweru and you need more valid structure for your table. this table column employee_id,scheme_id,and the payments_amounts is not suitable for looping task from controller, this table is lack of unique key. even key employee_id is not unique (based on your info, the row in this table can have more than 1 row with same employee_id), so I can say it will not give the result what you want.

1 like
tangtang's avatar
tangtang
Best Answer
Level 6

@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,
      ]);       
}
1 like

Please or to participate in this conversation.