Home > Net >  The POST method is not supported for this route. Supported methods: PUT. LARAVEL
The POST method is not supported for this route. Supported methods: PUT. LARAVEL

Time:12-22

I would like to send data with PUT method However, this error happens.

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The POST method is not supported for this route. Supported methods: PUT.

I don't really understand why my code is not correct.

web.php

Route::get('/', function () {
    return redirect('/home');
});

Auth::routes();
Route::put('/save_data', 'AbcController@saveData')->name('save_data');

view.blade.php

        <form action="{{route('save_data')}}" method="POST">
          @method('PUT')
          @csrf
          <input type = "hidden" name = "type" value ='stack' >
         
          <div>
            <button>post</button>
          </div>
        </form>

when it is changed

          <input type="hidden" name="_method" value="PUT">
          <input type="hidden" name="_token" value="{{ csrf_token() }}">

instead of

          @method('PUT')
          @csrf

It works well.

CodePudding user response:

make sure to check First Another Route with Same Name You can make POST route and dont need to put this after form tag @method('PUT')

CHange the Placement of the route before this Auth::routes();

use save_data in route instead of /save_data.

use {{url('save_data')}} in action instead of {{route('save_data')}}.

CodePudding user response:

change this

<button type="submit">post</button> instead of <button>post</button>

CodePudding user response:

If you insist on using PUT you can change the form action to POST and add a hidden method_field that has a value PUTand a hidden csrf field (if you are using blade then you just need to add @csrf_field and {{ method_field('PUT') }}). This way the form would accept the request. You can simply change the route and form method to POST. It will work just fine since you are the one defining the route and not using the resource group

after all this run artisan command

php artisan route:clear
  • Related