Laravel 使用 QueryBuilder 更新多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45809840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:31:59  来源:igfitidea点击:

Laravel Update Multiple Columns with QueryBuilder

laravellaravel-5.4

提问by Martney Acha

I'm using Query builder, I successfully update na first column but on the second query the change doesnt happen, I already checked the view part the name of input and its correct. here is my code.

我正在使用查询构建器,我成功更新了第一列,但是在第二个查询中没有发生更改,我已经检查了视图部分的输入名称及其正确性。这是我的代码。

  DB::table('area')
        ->where('id', $request->get('area_id'))
        ->update(['island_group_id' => $request->get('island_group_id')],
                 ['region_id'       => $request->get('region_id')]);
        return 'test';

回答by Nikita

$updateDetails = [
    'island_group_id' => $request->get('island_group_id'),
    'region_id' => $request->get('region_id')
];

DB::table('area')
    ->where('id', $request->get('area_id'))
    ->update($updateDetails);

回答by Kuldeep Mishra

DB::table('area')
    ->where('id', $request->get('area_id'))
    ->update([
             'island_group_id' => $request->get('island_group_id'),
             'region_id'       => $request->get('region_id')
            ]);
    return 'test';

回答by Mr. Taumal

I think it will be helpful to you.

我想它会对你有所帮助。

$area_id = $request->get('area_id');
$island_group_id = $request->get('island_group_id'); 
$region_id = $request->get('region_id');

$update_details = array(
    'island_group_id' => $island_group_id
    'region_id'       => $region_id
);

DB::table('area')
    ->where('id', $area_id)
    ->update($update_details);

回答by AddWeb Solution Pvt Ltd

Because you use every time new array for update field. Please use one array for update multiple field like:

因为您每次都使用新数组作为更新字段。请使用一个数组来更新多个字段,例如:

DB::table('area')
        ->where('id', $request->get('area_id'))
        ->update(array(
                  'island_group_id'=>$request->get('island_group_id'),
                  'region_id'=>$request->get('region_id')
         ));