如何在删除 Laravel 一对多关联模型时触发子模型事件?
一对多关系,Post 拥有多个 Comment。
Comment 模型拥有监控类 CommentObserver:
// CommentObserver.php
public function deleted(Comment $comment)
{
// 重新计算评论作者的 『发布评论数』
$comment->user->recalculateCommentCount();
}
是否可以直接使用以下方法,来触发 CommentObserver 里的事件监控?
// PostObserver.php
public function deleted(Post $post)
{
$post->comments()->delete();
}
推荐文章: