Error in laravel when doing post Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

2

I'm doing a simple CRUD in laravel 5.5 along with laravel colective but simply do not send me the post in fact or acknowledge the request:

There are my routes

 Route::get('/admin/products/{id}/edit', 'ProductController@edit')->name('EditarRegistro');//Formulario de Edicion // EL NAME ES PARA laravel colective
Route::post('/admin/products/{id}', 'ProductController@update')->name('productos.update'); //Formulario Actualizar

This is my controller using edit and update

public function edit($id)//este id permitira buscar el id del producto seleccionado y una vez encontrado se pasara a la vista
{

    $BuscarProducto = Producto::find($id);
    $buscarcategoria = Categoria::find($BuscarProducto->category_id);


    return view('admin.products.edit')->with('RenombradoBuscarVista', $BuscarProducto)->with('RenombradoCategoriaVista', $buscarcategoria);

}


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

    //return view();  // resgistart un nuevo producto a la base de datos
    dd($request);

    $Categoria = Categoria::find($id);
    $Categoria->name = $request->namec;  //el namec es el nombre dl input
    $Categoria->save();   // para guardar el registro
    $idCategoria = $Categoria->id; //sacar el ultimo id de la insersion de categoria

    $Producto = Producto::find($idCategoria );
    $Producto->name = $request->input('name');
    $Producto->description = $request->input('description');
    $Producto->long_description = $request->descripcionl;
    $Producto->price = $request->input('price');
    $Producto->category_id = $idCategoria;
    $Producto->save(); //insert
    $idProducto = $Producto->id;


    $Imagen = ProductImage::find($idProducto);
    $Imagen->product_id = $idProducto;
    $Imagen->save();  

    return redirect('/admin/products');

}

and here is my view as I mention utlizo laravel colective

{!!Form::model($RenombradoBuscarVista, ['method' => 'PUT', 'action' => ['ProductController@update',$RenombradoBuscarVista->id]]) !!}
        <div class="row">
        <div class="col-sm-6>
        <div class="form-group label-floating">
            <label class="control-label">Nombre del producto</label>
        <input type="text" class="form-control" name="name" value="{{$RenombradoBuscarVista->name}}">
        </div>
        <div class="form-group label-floating">
                <label class="control-label">Categoria</label>  
                <input type="text" class="form-control" name="namec" value="{{$RenombradoCategoriaVista->name}}">
            </div>
        </div>

        <div class="col-sm-6">
                <div class="form-group label-floating">
                    <label class="control-label">Descripcion</label>
                    <input type="text" class="form-control" name="description" value="{{$RenombradoBuscarVista->description}}">
        </div>




                <div class="form-group label-floating">
                    <label class="control-label">Precio del Producto</label>
                    <input type="number" class="form-control" name="price" value="{{$RenombradoBuscarVista->price}}">
        </div>



        <textarea class="form-control" placeholder="Descripcion extensa del producto"  name="descripcionl" >{{$RenombradoBuscarVista->long_description}}</textarea>

        {!!Form::submit('Guardar cambios',array('class' => 'btn btn-primary'))!!}

        <a href="{{ url('/admin/products')}}" class="btn btn-default">Cancelar</a>





{!!Form::close()!!}

It is worth mentioning that if you send me the view, the error appears when I click on the update button.

    
asked by Julio Vásquez Díaz 27.03.2018 в 05:27
source

3 answers

0

You are using the verb PUT in the form:

{!!Form::model($RenombradoBuscarVista, ['method' => 'PUT', 'action' => ['ProductController@update',$RenombradoBuscarVista->id]]) !!}

But on the route you are waiting for a POST:

Route::post('/admin/products/{id}', 'ProductController@update')->name('productos.update');

Hence, the error is "Method not allowed".

Laravel suggests using PUT / PATCH for the editions, so you should modify the route:

Route::put('/admin/products/{id}', 'ProductController@update')->name('productos.update');
    
answered by 27.03.2018 / 05:40
source
0

Reviewing the code you should improve this

You use PUT in the form:

{!!Form::model($RenombradoBuscarVista, ['method' => 'PUT', 'action' => ['ProductController@update',$RenombradoBuscarVista->id]]) !!}

The route is waiting for a POST:

Route::post('/admin/products/{id}', 'ProductController@update')->name('productos.update');

Hence, the error is "Method not allowed".

replace it with

Route::put('/admin/products/{id}', 'ProductController@update')->name('productos.update');

Also in the controller you perform this search

$Producto = Producto::find($idCategoria );

and I suppose that the category is a field of the product and not its primary key, so you should use this line:

$Producto = Producto::where('category_id', $idCategoria)->get();

The same applies when saving the image

is this way:

$Imagen = ProductImage::find($idProducto);
$Imagen->product_id = $idProducto;
$Imagen->save();  

It would be better if:

$Imagen = ProductImage::where('product_id',$idProducto)->get();
$Imagen->product_id = $idProducto;
$Imagen->save();  
    
answered by 27.03.2018 в 22:41
0

Sometimes it happens that you refresh directly the URL where you have to receive the post, and for what it is, expired session or simply that you enter the URL directly without having gone through the form before and it gives you that error, arriving at that url from where the data is filled, in this case I would solve the error ... By the way if someone knows how to detect this that illuminates us with the code ...

    
answered by 21.11.2018 в 11:29