0
votes

I use laravel 5.8 for my application. I have variables call "lot" and when I delete one, I want to perform other actions.

So I use deleting function on my model, everything works OK.

But know I have a function on my controller to delete many "lots" and I want actions to be perform only when all "lots" have been delete and not on every "lots" delete.

So I wonder if there is a way to achieve this ? Maybe we can pass a variable to boot functions to trigger or not the function ?

My model looks like this :

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

    static::deleted(function($modele) {

        Etage::doesntHave('lots')->delete();

    }
 }
1

1 Answers

2
votes

Model events are been designed to work for single models.

You should use Laravel Events instead, not tied to Models.

In EventServiceProvider.php register a new Event/Listener in the $listens array, like

LotsDeleted::class => PerformOtherAction::class

Create those classes with php artisan event:generate

Then, when you have finished deleting all your Lot objects, trigger the event with

event(new LotsDeleted());

The handle() function of your listener will be called and you can perform other actions.