Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to delete model in Laravel using Eloquent

I'm trying to delete a database entry through the associated model using ->delete(), but the entry is not deleted.

I tried changing the form from POST to DELETE, but that made no difference. And research around SO didn't yield anything either.

Prefered I would like to avoid using Model::destroy($ids), which does seem to work.

The route methods:

// Delete an asset
public function getDelete($id)
{
    // Try to find the asset, throw exception if not found
    $asset = Asset::findOrFail($id);

    $this->layout->title = 'Delete an asset';
    $this->layout->content = View::make('asset.delete')
            ->with('assetID', $id);
}

// Delete the actual asset
public function postDelete()
{
    $id = Input::get('AID');

    // Try to find the asset, throw exception if not found
    $asset = Asset::findOrFail($id);

    // Attempt to delete it
    if($asset->delete())
    {
        return Redirect::route('asset')
                ->with('success', 'The asset has been deleted succesfully!');
    }

    return Redirect::route('asset')
            ->with('failure', 'The asset could not be deleted. Please try again.');
}

The routes:

/* Route to deleting an asset
 * Shorthand: URL::route('deleteAsset')
 * Uses AssetController::getDelete
 */
Route::get('/asset/delete/{id}', array(
    'as' => 'deleteAsset',
    'uses' => 'AssetController@getDelete'
));

/* Route to actually deleting an asset
 * Shorthand: URL::route('deleteAssetPost')
 * Uses AssetController::postDelete
 */
Route::delete('/asset/delete/', array(
    'as' => 'deleteAssetPost',
    'uses' => 'AssetController@postDelete'
));

The model:

class Asset extends Eloquent
{
    public $timestamps = false;

    protected $table = 'Asset';

    protected $primaryKey = 'AID';

    protected $fillable = array('ACID', 'AKID', 'AATID', 'APurchaseDate');

    // Retrieve the customer's name associated with the asset
    public function customer()
    {
        return $this->hasOne('Customer', 'CID', 'ACID');
    }

    // Retrieve the asset type that is associated with the asset
    public function assetType()
    {
        return $this->hasOne('AssetType', 'ATID', 'AATID');
    }

    // Get the customer name
    public function getCustomerName()
    {
        return $this->customer()->first()->CName;
    }

    // Get the asset type name
    public function getAssetTypeName()
    {
        return $this->assetType()->first()->ATName;
    }

    // Get the associated key
    public function getKey()
    {
        return $this->AKID === -1 ? 'nvt' : $this->AKID;
    }

    // Get the purchase date in readable form
    public function getPurchaseDate($format = 'd-m-Y')
    {
        return date($format, strtotime($this->APurchaseDate));
    }
}

According to the controller the deletion is successful and it gives the success message.

No errors are thrown.

[edit]

Does anyone know what I'm missing here to make this work?

After logging the queries, the following queries are executed using $asset->delete():

select * from `Asset` where `AID` = ? limit 1
delete from `Asset` where `AID` = ?

I tried the same thing with Asset::destroy($id), results from that are:

select * from `Asset` where `AID` = ? limit 1
select * from `Asset` where `AID` in (?)
delete from `Asset` where `AID` = ?
like image 729
MisterBla Avatar asked Feb 20 '14 10:02

MisterBla


1 Answers

After a bit of discussion in chat we realised that the code was defining a getKey() method on the model which was used for a domain-specific purpose. However, this is actually a method that Eloquent defines on all models, and it uses it pretty heavily. In this case, Eloquent's delete method was calling getKey() to determine the model's ID.

The reason for the non-error was that although the delete itself was failing to delete (as it was trying to delete a model with ID of 'nvt'), it wasn't technically doing anything worth erroring about as it was perfectly valid code/SQl: DELETE FROM `table` WHERE `id` = 'nvt';

like image 86
alexrussell Avatar answered Oct 12 '22 07:10

alexrussell