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

Alagoro's avatar

laravel and lumen oferta::destroy(id) is not working

I'm going crazy:

public function destroy($id) { $resultado = Oferta::destroy($id);

    return redirect()->route('webofertas.index')
                    ->with('success', 'Oferta borrada: '.json_encode($resultado));
}

returns success, $resultado = 1 but mysql line is not deleted.

I tried the same with Lumen as an api and same result. Is something wrong with my MySQL?

TIA.

0 likes
16 replies
bobbybouwmann's avatar

The destroy method returns the count of the number of items that have been deleted. So in your case it makes sense it returns just 1 since that is the count.

If you want information from the model and output that in a message you should do this

public function destroy($id) 
{
    $oferta = Oferta::findOrFail($id);
    $oferta->delete();

    return redirect()->route('webofertas.index')
        ->with('success', 'Oferta borrada: '.json_encode($resultado));
}

Let me know if this works for you!

Alagoro's avatar

Thanks for replying.

$oferta = Oferta::findOrFail($id); $resultado = $oferta->delete();

return redirect()->route('webofertas.index')
    ->with('success', 'Oferta borrada: '.json_encode($resultado));

And now $resultado returns true, but the line is not deleting.

If I force an error:

$oferta = Oferta::findOrFail(111);

A 404 page returns, so value for $id exists as a primary key.

Alagoro's avatar

I try to give more info hopping it helps:

class Oferta extends Model { protected $connection = 'apimysql'; protected $fillable = [ 'IDOFERTAGESTION','FECHAALTA','IDSECCION','TEXTOCAB','MARCA','VENTA', 'ALQUILER','TRASPASO','OBSERVACIONES' ];

    public function seccion(){
    return $this->hasOne(Seccion::class, 'ID','IDSECCION');
}

}

Snapey's avatar

please format your code if you want help

Snapey's avatar

do you have soft deletes on the model?

Alagoro's avatar

Sorry. Code formats randomly. Is there a header-footer or similar?

Alagoro's avatar

I don't use the trait and deleted_at column is not created. delete returns true and if I use destroy() it returns 1 (1 record deleted)

I'm going crazy...

Snapey's avatar

format code with ``` on a line before and after each code block

Snapey's avatar

Both of your methods should delete the row. Are you sure you are looking at the right database and table?

Check that the $id is coming through correctly

Do you have and referential integrity set up in the database which would prevent deletion if there are related records?

Alagoro's avatar
'apimysql' => [
            'driver' => env('DB_CONNECTION_API'),
            'host' => env('DB_HOST_API', '127.0.0.1'),
            'port' => env('DB_PORT_API', '3306'),
            'database' => env('DB_DATABASE_API', 'forge'),
            'username' => env('DB_USERNAME_API', 'forge'),
            'password' => env('DB_PASSWORD_API', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
        ],

class Oferta extends Model {

    protected $connection = 'apimysql';
    protected $fillable = [
        'IDOFERTAGESTION',
        'FECHAALTA',
        'IDSECCION',
        'TEXTOCAB',
        'MARCA',
        'VENTA',
        'ALQUILER',
        'TRASPASO',
        'OBSERVACIONES',
    ];


It works with insert, and select.

I have no restrictions.

Alagoro's avatar

OK. I made an aPI with Lumen and this is the delete method in the controller:

public function deleteGestion($idgestion) {
        $resultado = Oferta::where('IDOFERTAGESTION', $idgestion);
        $oferta = $resultado->first();
        $result = false;
        if ($oferta) {
            DB::transaction(function() use($oferta, &$result) {
                if ($oferta->IDSECCION == K_SECCION_LOCAL) {
                    $result = Caraclocales::destroy($oferta->ID);
                } elseif ($oferta->IDSECCION == K_SECCION_NAVE) {
                    $result = Caracnaves::destroy($oferta->ID);
                }
//        
                $result = Oferta::destroy($oferta->ID);
            });
            return response(json_encode([$result, $oferta->ID, 'Deleted Successfully']), 200);
        } else {
            return response(json_encode([$idgestion, 'Id not found']), 404);
        }
    }

Transaction doesn't rollback. row from caraclocales model deletes ok, for ID=4 and response is 200, but the ID=4 row from ofertas is still there.

Snapey's avatar

You have tables with synchronised IDs? You delete from both models with the same ID

slev1n's avatar

Table's primaryKey is IDOFERTAGESTION or id? If first, you need reload default value

// Oferta.php
protected $primaryKey = 'IDOFERTAGESTION';

and then do ::destroy() or findOrFail() stuff.

Alagoro's avatar

First table has ID autonumeric. second table has a 1:0-1 relation so if I write to it, I set same ID. third table has another 1:0-1. When this EER was designed there was no Laravel and I'm trying to mantain it.

Alagoro's avatar

Thanks @SLEV-UA for your answer but ID is the primary key. IDOFERTAGESTION has a valid allownull property. It's a foreign-key that relates with table ofertas in the database Gestion.

Alagoro's avatar

I've found it!! It was so silly...

As all columns are uppercase ... primary key column has to be especified:

protected $primaryKey = 'ID';

Thanks to everybody. I hope this helps to someone else.

Please or to participate in this conversation.