我正在尝试更新我的用户个人资料,但遇到了问题
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。
我已经定义了路线,但我不确定为什么会发生这种情况。
web.php
Route::get('/user/{id}',[UserController::class,'show'])->name('user.profile');
Route::get('/edit/user/',[UserController::class,'edit'])->name('user.edit');
Route::post('/edit/user/',[UserController::class,'update'])->name('user.update');
UserController
public function update(Request $request)
{
$user = User::find(Auth::user()->id);
if ($user){
$validate = null;
if (Auth::user()->email === $request ['email']) {
$validate = $request->validate([
'name' => 'required',
'email' => 'required',
'phone' => 'required',
'address' => 'required'
]);
} else {
$validate = $request->validate([
'name' => 'required',
'email' => 'required',
'phone' => 'required',
'address' => 'required'
]);
}
if($validate){
$user->name = $request['name'];
$user->email =$request['email'];
$user->phone = $request['phone'];
$user->address = $request['address'];
$user->save();
$request ->session->flash('success', 'Profile have been updated');
return redirect()->back();
} else{
return redirect()->back();
}
} else {
return redirect()->back();
}
}
edit.blade.php
<form class="form-horizontal" action="{{route('user.update',$user->id)}}" method="POST">
@csrf
@method('PUT')
@if(session('success'))
<div class="alert alert-success" role="alert">
{{session('success')}}
</div>
@endif
<div class="card-body">
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" value="{{$user->name}}">
</div>
</div>