如何在Laravel中使用补丁请求?
2022-08-30 19:34:18
存在位于表中的实体User
Users
默认情况下,此表中的某些字段为空。
我需要更新这些字段并设置不为空的数据。
为此,我尝试在Laravel中使用方法:PATCH
路由:
Route::patch('users/update', 'UsersController@update');
控制器:
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
"name" => 'required|string|min:3|max:50',
"email_work" => 'email|max:255|unique:users',
"surname" => 'required|string|min:3|max:50',
"tel" => 'required|numeric|size:11',
"country" => 'required|integer',
"region" => 'required|integer',
"city" => 'required|integer'
]);
if ($validator->fails()) {
return response()->json(["message" => $validator->errors()->all()], 400);
}
$user = User::where("user_id", $id)->update([
"name" => $request->name,
"surname" => $request->surname,
"tel" => $request->tel,
"country" => $request->country,
"city" => $request->city,
"region" => $request->region,
"email_work" => $request->email
]);
return response()->json(["user" => $user]);
}
这是否意味着我可以传递任何数据进行更新?我应该将参数相对地传递给路由和控制器吗?$id
如何在Laravel中使用PATCH方法的正确处理程序?