Monday, February 12, 2018

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

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

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.

array img

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.



from Laravel Questions and Answers https://laravelquestions.com/php/how-to-update-multiple-rows-in-sql-from-array-data-using-laravel-eloquent/
via Lzo Media

No comments:

Post a Comment