0

My EventServiceProvider.php:

protected $listen = [
    'eloquent.deleted: *' => [
        'App\Listeners\EloquentListener',
    ],
    'eloquent.deleting: *' => [
        'App\Listeners\EloquentListener',
    ]
];

But its not working.

when I do it this way on my model file Its still not working:

public function boot()
{
    parent::boot();

    Event::listen(['eloquent.deleting: *'], function ($foo, $bar) {

        echo $foo;
    });
}

My laravel version is: 7.15

What is the problem? Do you have any idea?

omero
  • 93
  • 1
  • 4
  • 10
  • 7
    How do you perform the delete action? `When executing a mass delete statement via Eloquent, the deleting and deleted model events will not be fired for the deleted models. This is because the models are never actually retrieved when executing the delete statement.` – Mihai Matei Jun 16 '20 at 15:10
  • I am using soft delete and normal delete. my code is below: User::where('id', $id)->delete(); ModelHasRole::where('model_id', $id)->forceDelete(); – omero Jun 16 '20 at 15:16
  • 4
    That's exactly one of the cases when the delete event is not fired, because the delete is performed directly in the db. If you'd do it like `User::find($id)->delete()` it would fire the event because delete action is performed on the model. – Mihai Matei Jun 16 '20 at 15:18
  • @Mihai Matei thank you so muck your quick reply. When I use ->where('user_id', $ui) how can I fire? is it imposible? – omero Jun 16 '20 at 15:37
  • 2
    afaik you can't, unless you get the results first and you call the delete action afterwards: `User::where('id', $id)->first()->delete();` – Mihai Matei Jun 16 '20 at 15:57
  • 1
    User::where('id', $id)->get()->delete() is not working very bad. I dont want to use "first". – omero Jun 16 '20 at 16:28

0 Answers0