vendredi 3 juin 2016

Deleting the children models automatically

I have three models: User has-many Post has-many Comment. When I delete a user, I want all of his related posts to automatically be deleted as well as the comments that are related to these posts. In order to achieve this, I have the following code in User and Post models:

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

    static::deleting(function($user) {
        $user->posts()->delete();
    });
}

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

    static::deleting(function($user) {
        $post->comments()->delete();
    });
}

When I delete a user, all of his posts get deleted, however, the comments are preserved. Why is this happening?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1TSZKS6
via IFTTT

Aucun commentaire:

Enregistrer un commentaire