Monday, March 13, 2017

Laravel eloquent static boot deleting multiple relationship

I have this model that uses a static boot for deleting related models/tables in the db. First is the product model.

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

    static::deleting(function($product) {
        $product->hearts()->delete();
        $product->comments()->delete();
    });
}

It deletes the hearts and comments and it's working like a charm. But the problem is I have this reply model that has an relationship with my comment model and that reply model is being referenced from the comment model

public function replies()
{
    return $this->hasMany("App\Reply");
}

Then in my comment model I used another static boot for "Chaining" the boot delete from the product

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

    static::deleting(function($comment) { 
        $comment->replies()->delete();
    });
}

but it's not working. Can you guys give me an insight why this isn't working? Logically it should work because the comment is being deleted. Thank you.



via Keannu Alforque Atilano

Advertisement