jurandou
jurandou

Reputation: 134

Laravel observer - deleted and forceDeleted both fired

I use SoftDeletes trait in my model, and an Observer which works great for every event.

But when i forceDelete a model, both deleted and forceDeleted methods are fired, so my notifications are fired twice.

The only thread i found got no answers : https://github.com/laravel/framework/discussions/36191

Is there anyway to "quiet" the deleted method or something similar ?

Any help appreciated ! Julien

Upvotes: 2

Views: 2221

Answers (2)

Nicklas Kevin Frank
Nicklas Kevin Frank

Reputation: 6337

Since you are using both events, the only way is for you to check in your deleted event if it is coming from a force deletion and then stop further execution.

if ($model->isForceDeleting()) {
    return null; // Stop the rest of the event
}

Or as you mention, the reverse

if (! $model->isForceDeleting()) {
    // Deleted event code here
}

Upvotes: 3

sourabh
sourabh

Reputation: 7

i think you should comment that deleted function in observer. i think this will work try this. keep only one function there which is forceDeleted

Upvotes: 0

Related Questions