According to the accepted answer in Using Model Events Listener in Laravel 5, the following code should work normally:
class Question extends Model
{
public function answers()
{
return $this->hasMany(Answer::class);
}
// this is a recommended way to declare event handlers
protected static function boot() {
parent::boot();
// before delete() method call this
static::deleting(function($question) {
$question->answers()->delete();
});
}
}
However, after performing the following actions in php artisan tinker
:
$q = App\Question::create()
$q->answers()->create()
$q->delete()
The answer still persists in the database. It seems, that the event handler on Question model does not get triggered. How do I fix this?