存在實體User
,它被記錄在表中Users
如何在Laravel中使用修補程序請求?
默認情況下,此表中的某些字段爲空。
我需要更新這些字段並設置非空數據。
爲此,我嘗試使用PATCH
方法Laravel:
路由:
Route::patch('users/update', '[email protected]');
控制器:
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方法使用正確的處理程序?
你能運行'PHP工匠路線:list'向我們展示它告訴你關於你註冊這個特定的路線? –
如果你在update()函數中有$ id參數,那麼我想你需要添加'{id}'你的路由,就像這樣'Route :: patch('users/{id}/update','UsersController @ update ');' –