Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

basosneo's avatar

Products update; admin pannel

hi friends, I have a problem with the update method of my administration panel;

Instead of updating the data of the current products, this what it does is create new ones and it will not let me delete them later (neither the updated ones nor the previous ones).

It is as if he created clones that can not be erased, but with the variation of only one. I do not know if I can explain ...

productscontroller

 public function update(SaveProductRequest $request, Products $products)
    {
    $products->fill($request->all());
    $products->slug = str_slug($request->get('name'));
    $products->visible = $request->has('visible') ? 1 : 0;
    
    $updated = $products->save();
    
    $message = $updated ? 'Producto actualizado correctamente!' : 'El Producto NO pudo actualizarse!';
    
    return redirect()->route('products.index')->with('message', $message);
   }

public function destroy(Products $products)
{
    $deleted = $products->delete();
    
    $message = $deleted ? 'Producto eliminado correctamente!' : 'El producto NO pudo eliminarse!';
    
    return redirect()->route('admin.products.index')->with('message', $message);
}

web.php

Route::post('products/{products}', [
'as' => 'admin.products.update',
'uses' => 'ProductController@update'
 ]);

Route::delete('products/destroy/{products}', [
'as' => 'admin.products.destroy',
'uses' => 'ProductController@destroy'
]);

form

    {!! Form::model($products, array('route' => array('products.update', $products->slug))) !!}
                
                    <input type="hidden" name="_method" value="PUT">
                
                    <div class="form-group">
                        <label class="control-label" for="category_id">Categoría</label>
                        {!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
                    </div>
    
                    <div class="form-group">
                        <label for="name">Nombre:</label>
                        
                        {!! 
                            Form::text(
                                'name', 
                                $products->name, 
                                array(
                                    'class'=>'form-control',
                                    'placeholder' => 'Ingresa el nombre...',
                                    'autofocus' => 'autofocus'
                                )
                            ) 
                        !!}
                    </div>
                    
                    <div class="form-group">
                        <label for="extract">Extracto:</label>
                        
                        {!! 
                            Form::text(
                                'extract', 
                                $products->extract, 
                                array(
                                    'class'=>'form-control',
                                    'placeholder' => 'Ingresa el extracto...',
                                )
                            ) 
                        !!}
                    </div>
                    
                    <div class="form-group">
                        <label for="description">Descripción:</label>
                        
                        {!! 
                            Form::textarea(
                                'description', 
                                $products->description,
                                array(
                                    'class'=>'form-control'
                                )
                            ) 
                        !!}
                    </div>
                    
                    <div class="form-group">
                        <label for="price">Precio:</label>
                        
                        {!! 
                            Form::text(
                                'price', 
                                $products->price, 
                                array(
                                    'class'=>'form-control',
                                    'placeholder' => 'Ingresa el precio...',
                                )
                            ) 
                        !!}
                    </div>
                    
                    <div class="form-group">
                        <label for="image">Imagen:</label>
                        
                        {!! 
                            Form::text(
                                'image', 
                                $products->image, 
                                array(
                                    'class'=>'form-control',
                                    'placeholder' => 'Ingresa la url de la imagen...',
                                )
                            ) 
                        !!}
                    </div>
                    
                    <div class="form-group">
                        <label for="visible">Visible:</label>
                        <input type="checkbox" name="visible" {{ $products->visible == 1 ? "checked='checked'" : '' }}>
                    </div>
            
                    
                    
                    
                    <div class="form-group">
                        {!! Form::submit('Actualizar', array('class'=>'btn btn-primary')) !!}
                        <a href="{{ route('products.index') }}" class="btn btn-warning">Cancelar</a>
                    </div>
                
                {!! Form::close() !!}

edit; i cant delete any product

0 likes
11 replies
basosneo's avatar

The issue is that the page works for me with the slug parameter as I have it now .. and until I put the 'update' function it let me delete normal, now that it is active, the update function will not let me delete it and update me Create another product ..

I was reading the documentation that you gave me, if that function were not available. so. Should not my page work? And I did not understand in which model I should place that function (perhaps in the products.php?)

Snapey's avatar

more likely then that when you think you post to update you actually post to create method.

put dd() at the top of update and create (one at a time) and see which is hit when you save the form

Snapey's avatar

thinking more. if $products->slug comes back as an empty string then you will post to /products/ and this will go to the create method instead of update

basosneo's avatar

Well, I was looking at the documentation and added the function for the slug; I got the following error when giving the botton update:

MethodNotAllowedHttpException in RouteCollection.php line 233:

Try to remove the function and re-migrate everything to see if I went back to my previous error of updating but not being able to delete, but then nothing changes. I still have that error when giving the botton update

And if it was not enough, I can not delete any product

Snapey's avatar

we discussed before. rather than just randomly changing things you need to understand why it is doing what it is doing.

to do this you need to be able to debug issues

one approach is to review the source code in the browser so you can understand where the form might be posted.

the other approach is to look in your browser network tools and see what is being sent to the server. what the url looks like, what the headers are set to and what the form data looks like

Snapey's avatar
Snapey
Best Answer
Level 122

also... if you have hidden field method type of PUT then you need to use put in your web.php and not post

1 like
basosneo's avatar

thanks men, you are the master. its work now!

and can you explain more, how i can have a soft delete on my products?

Snapey's avatar

read the docs

implement

ask questions if it does not work

basosneo's avatar

@snapey I already read it and implemented it in the database and the seeders tables, it what is confuse to me is how to test if it is working

Snapey's avatar

You delete the record then you see that there is a date time in the deleted_at column of the database. Also, the record is no longer visible when you ->get() from the database.

Please or to participate in this conversation.