删除功能全部成功 但是所有错误的更新功能 如何解决
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');
答案 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."]);
}