如何在Laravel控制器中更新多行

3
基本上,我想要一次性或批量更新这个表格。
我想要更新这个表格中的“image_id”字段。

Table

这是我在控制器中的代码。
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';
  }

但由于某些原因它无法工作。如果有人能指出我在这里忽略了什么,那将是很好的。
谢谢。

1
你应该尝试使用 whereIn - lagbox
2
应该翻译为:Barcode::whereIn('id', $ids)->update(['image_id'=>$id]); - STA
谢谢大家,我没有想到那个。 - Edson
2个回答

6
你应该使用 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';
}

1
在Laravel中,如果您想要更新多个行或获取多个相同类型的行,请始终使用whereIn
文档将whereIn定义为- 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';
  }

链接到文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接