You can try using the eloquent deleting event. Tested with Laravel 4.2.16.
A test model example,
class TestModel extends \Eloquent {
// enable soft deleting
use SoftDeletingTrait;
// set table name
protected $table = "TestModel";
// adjust boot function
public static function boot()
{
// run parent
parent::boot();
// add in custom deleting
static::deleting(function($model)
{
// save custom delete value
$model->deleted = 1;
$model->save();
});
}
}
Because this still uses the deleted_at column, once records are marked as deleted future queries to the database with eloquent models will ignore them - if you don't have the ->withTrashed() as part of the query.
Also, since this is using the soft delete ability you can undelete records. However, I'm not finding a event to tie on too for restoring a soft-deleted record. Might be able to use the updating event and just check the fields for the deleted_at being set to null, and update the deleted to 0 at that time. Otherwise this could lead to records being marked with deleted=1 and the deleted_at timestamp equaling null. That is if you need to undelete a record.
Hope that helps.
Note: as zaalbarxx pointed out in http://laravel.io/forum/01-14-2015-restored-at-timestamp there actually is a restoring and restored event to hook in to.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community