由于不支持 PUT 方法而更新用户配置文件时出现问题 [Laravel]

我正在尝试更新我的用户个人资料,但遇到了问题

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>



qq_笑_17
浏览 88回答 1
1回答

HUWWW

从刀片文件中删除@method('PUT') -<form class="form-horizontal" action="{{route('user.update',$user->id)}}" method="POST">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @csrf&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @if(session('success'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="alert alert-success"&nbsp; role="alert">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{{session('success')}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-body">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="name" class="col-sm-2 col-form-label">Name</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-10">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" class="form-control" name="name"&nbsp; value="{{$user->name}}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
打开App,查看更多内容
随时随地看视频慕课网APP