hadis
hadis

Reputation: 39

update method issue(The POST method is not supported) in laravel

I want to create update method and this is the code:

Route::get("/allProducts/edit/{id}","AllproductController@edit")->name('Allproduct.edit');
Route::post("/allProducts/update/{id}","AllproductController@update")->name('Allproduct.update');
<form class="form-horizontal tasi-form" method="post" enctype="multipart/form-data"
                      action="{{ route('allProducts.update' , [ 'id'=>$allproduct->id ]) }}">
                    {{ csrf_field()}}

  public function update(Request $request, $id)
    {
       $data = Allproduct::find($id);

        $data->name = $request->name;
        $data->save();

        return redirect(route('allProducts.index'));
    }

when I click on submit button it shows me :

"The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE" error!

what is the problem?

Upvotes: 0

Views: 1500

Answers (2)

Piazzi
Piazzi

Reputation: 2636

As said here

HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:

So your form should look like this:

<form class="form-horizontal tasi-form" method="post" enctype="multipart/form-data" action="{{ route('Allproduct.update' , [ 'id'=>$allproduct->id ]) }}"> @csrf @method('PUT')

You had a typo in your route name and it was missing the method field.

Change your route to this:

Route::put("/allProducts/update/{id}","AllproductController@update")->name('Allproduct.update');

This will work, but i strongly recommend you read this laravel naming conventions, and then change the name of your controller and model to AppProductController, AppProduct.

Upvotes: 0

Katanji
Katanji

Reputation: 68

Your route names do not match.

in routes: name('Allproduct.update');

in the form: allProducts.update

Also, you can always check the name of the routes thanks to the console command:

php artisan route:list

if you want use method PUT:

you can change method in routes:

Route::post to Route::put

and add next in form:

<input type="hidden" name="_method" value="PUT"> 

OR

@method('PUT') 

this is if your laravel version is 6 and if your version another, check correct way to use PUT method in forms at laravel.com/docs/6.x/routing with your version.

Upvotes: 1

Related Questions