Home > Software design >  How to update multiple Rows In Laravel Controller
How to update multiple Rows In Laravel Controller

Time:01-11

basically I want to update this table in one go or bulk update.

I wanted to update the "image_id" field on this table.

Table

This is my code in the Controller

public function storebulk(Request $request,$id)
  {
     $ids= ['8','9','10']; //sent from the front-end

     $barcode = Barcode::where('id', $ids)->update(['image_id'=>$id]);

     return 'Done';
  }

but for some reason it doesn't work. if anyone can point out what I missed here it will be great.

thanks

CodePudding user response:

You should use whereIn:

public function storebulk(Request $request, $id)
{
     $ids= ['8','9','10']; //sent from the front-end

     $barcode = Barcode::whereIn('id', $ids)->update(['image_id'=>$id]);

     return 'Done';
}

CodePudding user response:

In Laravel, If you want to update multiple rows or get multiple rows of same type always use whereIn.

The Docs Define whereIn as - The whereIn method verifies that a given column's value is contained within the given array

public function storebulk(Request $request,$id)
  {
     $ids= ['8','9','10']; //sent from the front-end

     $barcode = Barcode::whereIn('id', $ids)->update(['image_id'=>$id]);

     return 'Done';
  }

Link to docs

  •  Tags:  
  • Related