4

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.

1
  • The array I am passing looks like this which array you are refering Commented Feb 12, 2018 at 11:19

3 Answers 3

1

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]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I never knew laravel can read the array automatically when you do the update. Thank you!
1

You can't do that with just one query, but you could do that with two queries. An example:

$check = categorylistcontent::query();
$notCheck = categorylistcontent::query();

foreach ($request->checklist as $item) {
    $query = $item['isCheck'] === 1 ? 'check' : 'notCheck';
    $$query->orWhere(function($q) use($item) {
        $q->where('checklistid', $item['checklistid'])->where('categoryid', $item['categoryid']);
    }
}

$check->update(['check' => 1]);
$notCheck->update(['check' => 1]);

I haven't tested this exact code, but I think it will be helpful for you to get the idea.

2 Comments

Will try this approach, Thank you!
How to update like this? ids: array (size=3) 0 => int 3 1 => int 1 2 => int 2 values: array (size=3) 0 => float 0 1 => float 0 2 => float 0
0

You need to update multiple rows by putting it in foreach loop

eg:

foreach($checklistdata as $key =>$value){
$dataset[] = ['checklistid'=>$id,'categoryid' => $key,'isCheck'=>$value];

categorylistcontent::where([['checklistid',$id], ['categoryid', $dataset=>['categoryid'] ]])
            ->update($dataset['isCheck']);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.