Tim van Uum
Tim van Uum

Reputation: 1903

Is there a force deleting event? - Laravel 5.2

When I throw records into the "trashbin" I use the normal delete method with softDelete enabled.

When I force delete a record I want to delete the belonging images as well. So I want to use Laravel's events. On forceDeleting I want some code to be executed.

What event can I call for that? When calling forceDeleting I get:

Call to undefined method Illuminate\Database\Query\Builder::forceDeleting()

What event should I use for this?

EDIT For now I'm using:

Document::deleting(function ($document) {
        if(!$document->deleted_at) {
            // normal delete
        }else{
            // force delete
        }
});

See also: https://codeneverlied.com/force-deleting-event-in-laravel/

But I still like to know if there is a event for this.

Upvotes: 3

Views: 869

Answers (3)

Pooria Honarmand
Pooria Honarmand

Reputation: 793

Better approach is this:

// GOOD approach
 static::deleted(function ($document) {
      if (Document::withTrashed()->where('id', $model->id)->exists()) {
           // document is soft deleted
      } else {
           // document is force deleted
      }
});

Upvotes: 0

Justin Kruskie
Justin Kruskie

Reputation: 422

According to the Eloquent documentation, these are the events:

retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, and restored.

So to answer your question of if it exists, that answer would be no. Not according to the documentation of Laravel 5.8

https://laravel.com/docs/5.8/eloquent

Hope I could help.

ImJT

Upvotes: 1

Edward Louth
Edward Louth

Reputation: 1060

You can create your own event and fire it yourself. Would be easiest to extend the existing Delete Event and maybe call it Force Deleting.

The next step would be to extend the softDeleting trait to fire this event for you automatically.

Upvotes: 1

Related Questions