Summer Sale! All accounts are 50% off this week.

LTroya's avatar
Level 20

Deleting model observer not beign fired

Hello guys,

For some reason, the deleting model event is not being called after deleting a model instance.

Here is my code:

class Activity extends Model
{
    protected static function booted()
    {
        parent::booted();

        self::deleting(function ($model) {
            dd('Firing deleting event!');
            $model->annotations()->delete();
        });
    }
}

This is my test:

/** @test */
    function it_should_delete_annotations_when_deleted()
    {
        $activity = Activity::factory()->create();

        $activity->annotations()->create(['x' => 10, 'y' => 10]);
        
        $activity->delete();

        $this->assertCount(0, ImageAnnotation::where('annotable_id', $activity->id)->where('annotable_type', Activity::class)->get());
    }

I am out of ideas, can you please give some hints? I don't see any difference with the documentation: https://laravel.com/docs/8.x/eloquent#events-using-closures

I am using Laravel 8.83.5

Kind regards

0 likes
9 replies
okusax's avatar

hi @ltroya in the documentation it uses static like:

static::deleting(function ($model) {
///
}

but you are using self:

self::deleting(function ($model) {
///
}

can you try changing that?

okusax's avatar

@LTroya mmm also in the documentation this line is not necessary:

parent::booted();

but i don't think this would change anything.

Maybe the error is related to events on testing runtime only. Can you try to run the test adding fresh() to $activity after the create statement?

$activity = Activity::factory()->create();
$activity->fresh();
LTroya's avatar
Level 20

@okusax I am not sure if that will trigger events such as creating, deleting, and so on...

The weird thing is that is working on production, same code, no changes at all.

MohamedTammam's avatar

I'm not sure whether the get returned value will work here or not, try to use assertDatabaseMissing instead.

$id = activity->id;
$activity->delete();
 $this->assertDatabaseMissing('image_annotations', ['annotable_id' => $id]);
LTroya's avatar
Level 20

@MohamedTammam the issue is that the model event is not deleting the record. But you are right, that assertion method is better than the way I am doing it

LTroya's avatar
Level 20

@MohamedTammam sorry for taking so long to get back to you. I have reviewed the discussion on GitHub and tried the solution you posted but it is not working

This is what I have right now on my setUp method

protected function setUp(): void
    {
        parent::setUp();

        // add this to remove all event listeners
        Activity::flushEventListeners();

        // reboot the static to reattach listeners
        Activity::boot();

        $this->activity = Activity::factory()->create();
    }
mikailfaruqali's avatar

i have same issue just use event fired when use find() function like

Activit::find(1)->delete();

Please or to participate in this conversation.