需要使用Laravel Eloquent一次更新多条记录

时间:2017-09-07 16:26:25

标签: php database laravel-5 eloquent

我只需要及时更新多行。

一次可以插入多行,如下面的代码,

foreach ($request->something as $key => $value) {
    $data=array(
        'db_table_field0'=> $request->form_something[$key],
        'db_table_field1'=>$request->form_something2[$key],
        'db_table_field2'=>$request->form_something3[$key],
        'db_table_field3'=>$request->form_something4[$key],
        );
        tableModel::insert($data);
 }

数据正在插入,我尝试了同样的更新,如

foreach ($request->something as $key => $value) {
    $auc_det_id = tableModel::where('value', $request->value[$key])->get();

    $data=array(
        'db_table_field0'=> $request->form_something[$key],
        'db_table_field1'=>$request->form_something2[$key],
        'db_table_field2'=>$request->form_something3[$key],
        'db_table_field3'=>$request->form_something4[$key],
        );
        $auc_det_id->update($data);
 }

它没有更新,没有找到类似方法更新的错误。我还尝试了save()而不是update()

我检查了null $auc_det_id是否数据未保存(无错误)

1 个答案:

答案 0 :(得分:2)

您可以尝试:

$data=array(
    'db_table_field0'=> $request->form_something[$key],
    'db_table_field1'=>$request->form_something2[$key],
    'db_table_field2'=>$request->form_something3[$key],
    'db_table_field3'=>$request->form_something4[$key],
);

tableModel::where('value', $request->value[$key])->update($data);