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($post) {
$post->comments()->delete();
});
}
When I delete a user, all of his posts get deleted, however, the comments are preserved. Why is this happening?