Symfony Component Httpkernel Exception Methodnotallowedhttpexception The PUT method is not supported for this route. Supported methods: GET, HEAD, POS

Asked

Viewed 45 times

-2

My Blade

  <form action="{{route('Update.afiliados', $users->id)}}" method="POST">
            @csrf
            @method('put') 
        <button  class="btn btn-block btn-flat btn-primary" method="post"> 
            <span class="fas fa-user-minus"></span>
            ATUALIZAR
        </button>
        </form>
  </form>

My Controller

public function update(Request $request)
{        if (!$users = users::find($id))
    {
        return redirect('Afiliados');
    }
    
    //$users = users::where('id', $id)->first();
     $users = users::findOrFail($request->id)->update($request->all());
     
    dd('TA OK');
}

my route

Route::put('/Afiliados/{id}', [App\Http\Controllers\AfiliadosController::class, 'update'])->name('Update.afiliados');
  • When does this error occur? When submitting the form? What version of the Standard? These are details that help answer the question.

3 answers

-1

Hello! I believe he is waiting for the ID argument, which you are passing but is not identified as inside the form:

//Form

<form action="{{route('Update.afiliados', ['id' => $users->id])}}" method="POST">
    @method('put')      
    @csrf

//Controller

public function update(Request $request,$id){}

Consider using the automated route functions of the Variable, it automatically creates routes and controllers:

https://laravel.com/docs/8.x/controllers#Resource-controllers

-1

The correct would be something like this: Blade

<form action="{{route('Update.afiliados', $users->id)}}" method="PUT">
  @csrf
           
  <button type="submit" class="btn btn-block btn-flat btn-primary"> 
    <span class="fas fa-user-minus"></span>ATUALIZAR
  </button>
</form>

controller :

public function update(Request $request,$id){
}
  • There is no method="PUT" using html form, the correct is how it did using @method('put')

-1

The error is here

    if (!$users = users::find($id))
    {
        return redirect('Afiliados');
    }

You didn’t define the $id in the update method, that way the error is released because the browser is trying to do PUT on the route he was redirected here redirect('Afiliados')

Solution would be public function update(Request $request, $id) and avoid redirecting if you do not find the registration, because the browser will try to do the PUT where it was redirected, you can avoid this by using users::findOrFail($id)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.