如何多次更新行laravel

时间:2018-09-12 11:51:01

标签: php laravel adminlte

删除功能全部成功 但是所有错误的更新功能 如何解决

Error

public function deleteAll(Request $request)
{
    $ids = $request->ids;
    DB::table("target")->whereIn('id', explode(",", $ids))->delete();
    return response()->json(['success'=>"Products Deleted successfully."]);
}
    public function updateAll(Request $request, $id)
{
    $ids = $request->ids;
            DB::table('target')->whereIn('id',$id)->update(['first_name'=>'test']);
    return response()->json(['success'=>"Products update successfully."]);
}

路线:

Route::delete('myproductsDeleteAll', 'AdminController@deleteAll');
Route::post('myproductsUpdateAll', 'AdminController@updateAll');

错误 https://pastebin.com/9tA2VDpB

2 个答案:

答案 0 :(得分:1)

您可以尝试以下代码

public function updateAll(Request $request, $id)
{
    $ids = $request->ids;
    foreach( $ids as $id ){          
      DB::table('target')->where('id', $id)->update(['first_name'=>'test']);
    }
    return response()->json(['success'=>"Products update successfully."]);
}

希望这会有所帮助。

答案 1 :(得分:0)

您在updateAll函数中出错,未定义$id并且存在$ids,您需要像在deleteAll函数中那样将其爆炸

public function updateAll(Request $request, $id)
{
    $ids = $request->ids;
        DB::table('target')->whereIn('id',explode(",", $ids))->update(['first_name'=>'test']);
    return response()->json(['success'=>"Products update successfully."]);
}