Home » Php » Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException in update

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException in update

Posted by: admin October 29, 2018 Leave a comment

Questions:

When I click on update button I get next error:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message

For update I have next routes:

Route::post('edit/user/{id}','[email protected]');
Route::get('edit/user/{id}','[email protected]');

As you see, I use post method for update.

My controller code:

public function edit($id){
            $user = User::where('id',$id)
            ->first();
            return view('user.edit', compact('user','id'));
        }

        public function update(Request $request, $id){
            $user = new User();
            $data_user = $this->validate($request,$rules_user);
            $data_user['id'] = $id;
            $user->updateUser($data_user);
            return redirect('/users');
        }

My update.blade.php code:

   @extends('layouts.app')

@section('content')
<div class="container">
@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div><br />
@endif
    <div class="row">
    <form method="post" action="{{action('[email protected]', $id)}}" >
        {{csrf_field()}}
        <input name="_method" type="hidden" value="PATCH">
        <div class="form-group">
       <input type="hidden" value="{{csrf_token()}}" name="_token" /> 
            <label for="name">User name:</label>
            <input type="text" class="form-control" name="name" value={{$user->name}} />
        </div>
        <button type="submit" class="btn btn-primary">Update</button>
        </form>
    </div>
</div>
@endsection
Answers: