简体   繁体   中英

Laravel 5.6 Error MethodNotAllowedHttpException

i am trying to edit default Auth user in laravel and i get the error when i submit my edit form so here is my controller :

class UserController extends Controller
{

public function __construct()

{
    $this->middleware('auth');
}

public function edit(User $user)
{
    $user = Auth::user();
    return view('admin.profile.edit', compact('user'));
}

public function update(User $user)
{
    $this->validate(request(), [
        'name' => 'required',
        'email' => 'required|email|unique:users',
        'password' => 'required|min:6|confirmed'
    ]);

    $user->name = request('name');
    $user->email = request('email');
    $user->password = bcrypt(request('password'));

    $user->save();

    return back();
}
}

and here is my view file when of form i just put the form here

    <form method="post" action="{{route('users.edit', $user)}}">
                    {{ csrf_field() }}
                    {{ method_field('patch') }}
                    <input type="text" name="name"  value="{{ $user->name }}" />

                    <input type="email" name="email"  value="{{ $user->email }}" />

                    <input type="password" name="password" />

                    <input type="password" name="password_confirmation" />

                    <button type="submit">Send</button>
                </form>

so the important file which i 90% sure that the problem is with is my route i know that i am some how sending some get to post or vise i am really confused with this part

Route::get('admin/profile/{user}',  ['as' => 'users.edit', 'uses' => 'UserController@edit']);
Route::post('admin/profile/{user}/update',  ['as' => 'users.update', 'uses' => 'UserController@update']);

so now when i submit the form i get this error

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message

and btw i have tried to use patch route with hidden input too but yet again same out put .

ok So found the problem i have to change the user edit route to user update so

<form method="post" action="{{route('users.edit', $user)}}">

would be changed to this

<form method="post" action="{{route('users.update', $user)}}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM