abdul hannan zaroo
abdul hannan zaroo

Reputation: 11

MethodNotAllowedHttpException in RouteCollection.php line 233: Laravel 5.4

I am facing a MethodNotAllowedHttpException error. here is my code:

In the routes file:

Route::put('updateitem/{rowId}',[
'as'=>'item.update',
'uses'=> 'CartController@UpdateItem'
]);

In the index.blade file:

<div class="form-inline">
   <form action="{{ route('item.update',$item->rowId) }}" method="PUT">
       <div class="form-group">
          <input type="number" value="{{$item->qty}}" class="form-control" name="qty">
          <input type="submit" class="btn btn-default btn-sm" value="Update">                                       
       </div>
   </form>
</div>

In the controller file:

public function UpdateItem(Request $request, $rowId)
   {
    # code...

   }

I have tried changing the route to POST as well with no luck. Can someone Please help me ! Thanks.

Upvotes: 0

Views: 2803

Answers (1)

Saleem MH
Saleem MH

Reputation: 31

As mentioned in Laravel documentation:

Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need to add a hidden _method field to spoof these HTTP verbs.

The method_field helper can create this field for you:

{{ method_field('PUT') }}

Example from Laravel documentation:

<form action="/foo/bar" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

In addition to that you forgot to add _token field to your form, which indeed will throw an exception error, to solve it you can add the field as mentioned in the example above, or just by adding:

{{ csrf_field() }}

So at the end your code will be:

<div class="form-inline">
    <form action="{{ route('item.update',$item->rowId) }}" method="POST">
       <div class="form-group">
           <input type="number" value="{{ $item->qty }}" class="form-control" name="qty">
           <input type="submit" class="btn btn-default btn-sm" value="Update">     
       </div>
       {{ method_field('PUT') }}
       {{ csrf_field() }}
   </form>
</div>

And the rest will be the same :)

Upvotes: 2

Related Questions