我正在使用 Laravel 开发一个数据库驱动的网站。现在我在更新数据库时遇到问题。我想运行批量更新。通常我是这样跑的。
$data = [
[
'col_1'=>$value1
],
[
'col_1'=>$value2
],
[
'col_1'=>$value3
],
[
'col_1'=>$value4
]
];
MyTableObject::update($data)->where('col_2'=>$col_2_val);
正如您在上面的代码中看到的那样,where 子句只检查要更新的所有行的一个条件。但我想要的是每行或查询都需要不同的 where 子句条件。要对每一行使用 foreach 并运行查询,这将非常耗时,因为我必须更新很多行。为了证明这一点,它将是这样的。
$data = [
[
'col_1'=>$value1,
'col_2'=>$where_value_1 // This is not the column to be updated. That is for where clause.
],
[
'col_1'=>$value2,
'col_2'=>$where_value_2 // This is not the column to be updated. That is for where clause.
],
[
'col_1'=>$value3,
'col_2'=>$where_value_3
],
[
'col_1'=>$value4,
'col_2'=>$where_value_4
]
];
MyTableObject::update($data)->where('where_col_name'=>'col_2');
我找到了这个链接,但答案并不清晰和完整。是否可以在 Laravel 中做到这一点,我该怎么做?