Skip to content
Advertisement

Determine if is a soft delete in a event handler Laravel

I have this event handler:

protected static function boot() {
    parent::boot();

    static::deleting(function($user) { // before delete() method call this
        $user->comments()->delete();

    });
}

When I use $user->forceDelete(); and $user->delete(); this event is triggered and delete all comments. This is not ok because I want this event to be triggered only on $user->forceDelete();. In my case the other tables does not have soft delete implemented

Advertisement

Answer

You can check for the forceDeleting property on the model. This will be set (and true) if you’re performing a forceDelete

static::deleting(function($user) { // before delete() method call this
    if ($user->forceDeleting) {
        $user->comments()->delete();
    }
});
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement