Model Events not firing
djekl opened this issue · comments
I recently found this, and it perfectly handles what I wanted to do. However one problem with it is that it doesn't fire (at least) the eloquent.deleted
event.
If you look at this issue - laravel/framework#2536 I was originally thinking that it was this that was the problem. However if you do it as the issue explains;
Analytic::where('id', 2)->first()->delete();
or
$analytics = Analytic::where('id', '>', 100)->get();
foreach ($analytics as $analytic) {
$analytic->delete();
}
or
Analytic::where('id', '>', 100)->get()->each(function($analytic) {
$analytic->delete();
});
Basically loading the data into memory by calling first()
or using the each
function on the collection.
Unfortunately the only way I have found so far to get the event to fire was to manually check the boolean response of delete()
and then fire the event myself;
$analytic = Analytic::where('id', 100)->first();
$deleted = $analytic->delete();
if ($deleted) {
event("eloquent.deleted: " . Analytic::class, [
$analytic,
]);
}
return [
"success" => $deleted,
];
I don't know if the other events fire at the moment, but at least the deleting
and deleted
events aren't firing.
I can't see how this library would interfere with the firing of delete events. If you have a hook that would cause the deleting
event to fail, that would prevent the execution chain from reaching delete
.