我想在Laravel更新查询的Where子句中推送一个数组。
这是更新查询。
DB::table('users')->where('id', 1)->update(array('votes' => 1));
是否可以使用下面这样的查询??
$array_of_ids;
DB::table('users')->where($array_of_ids)->update(array('votes' => 1));
感谢
只需使用whereIn
:
$array_of_ids;
DB::table('users')->whereIn('id', $array_of_ids)->update(array('votes' => 1));
请仔细阅读文档。在这种情况下,所有类型的where
语句都记录在这里:Query Builder-Selects
尝试此查询:
DB::table('users')->whereIn('id', $array_of_ids)->update(['votes' => 1]);
使用型号:
User::whereIn('id', $array_of_ids)->update(['votes' => 1]);
使用查询生成器:查询生成器还可以使用update方法更新现有记录。update方法与insert方法一样,接受包含要更新的列的列和值对的数组。您可以使用where子句约束更新查询:
DB::table('users')
->where('id', 1)
->update(['votes' => 1]);