Laravel MethodNotAllowedHttpException error en route Resource

2

I have a question, when I try to update a data I get the MethodNotAllowedHttpException error, I understand that this error comes out when there is a problem with the paths that are get and should be post but in my case my path is resource :

web.php

Route::resource('inventario', 'InventarioController');

Controller (update function)

   public function update(Request $request, $id)
   {
       $this->validate($request,[ 'cantidad'=>'required']);
       Inventario::find($id)->update($request->all());
       return redirect()->route('inventario.show')->with('success','Registro actualizado satisfactoriamente');
   }

Vista (show.blade.php)

<form method="POST" action="{{ route('inventario.update', $inv->id_inventario )}}" role="form">
     {{ csrf_field() }}
     <div class="form row">
         <input type="hidden" name="id_inventario" class="form-control input-sm" id="id_inventario"
                                   value="{{ $inv->id_inventario }}">
         <input type="number" name="cantidad" class="form-control input-sm" id="cantidad"
                                   value="{{ $inv->cantidad }}">
     </div>
     <div class="form-group col-md-6">
         <input type="submit" value="Guardar" class="btn btn-success">
         <button type="button" class="btn btn-white" data-dismiss="modal">Cerrar</button>

     </div>
 </form>

In my case when clicking on the update button, a modal opens, which is where the data I want to update appears.

    
asked by Daniel Gironás 04.12.2018 в 06:33
source

1 answer

1

In your view, your form should contain the following to indicate that it is an update action.

@method('PUT')

This verb PUT will serve to indicate that it is an update action; this goes on your form; since in the forms; The only methods that can be specified are POST and GET

Now you should declare the route separately, in order to distinguish one action from the other

Route::put('/inventario', 'InventarioController@update');

The previous route should go just before the declaration of your ResourceController ; As you can see at the end with the operator @ I indicate the name of the method I want to access, in this case the% of update

    
answered by 04.12.2018 / 06:45
source