Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update multiple rows in sql from array data using laravel eloquent

Tags:

php

mysql

laravel

I was trying to update multiple records in my database using laravel eloquent but is getting errors when trying to update using an array.

I am not really sure how to correctly get the data from the array to my update function.

The array I am passing looks like this.

enter image description here

My Database table looks like

id | checklistid | categoryid | isCheck | created_at | updated_at

My Controller looks like this.

public function updateCategoryListData(Request $request){
    $checklistdata = $request->get('checklist');
    $id = $request->get('checklistid');
    $dataset = [] ;
    foreach($checklistdata as $key =>$value){
                $dataset[] = ['checklistid'=>$id,'categoryid' => $key,'isCheck'=>$value];
           }
        categorylistcontent::where([['checklistid',$id], ['categoryid', $dataset=>['categoryid'] ]])
            ->update($dataset['isCheck']);
}

Would you be able to advise how I can use the array to get the 'checklistid' and 'categoryid' to be used as the where clause of the update statement and then the 'isCheck' to be set in the update.

like image 890
Metono Avatar asked Oct 29 '25 16:10

Metono


1 Answers

You don't need dataset array, rather do the following:

foreach($checklistdata as $key =>$value){
    categorylistcontent::where('checklistid',$id)->where('categoryid',$key)
        ->update(['isCheck'=>$value]);
}
like image 141
Sohel0415 Avatar answered Oct 31 '25 05:10

Sohel0415



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!