不修改 user notify 方法,简单粗暴解决自己通知自己问题
notify 方法的职责是通知,不应该有判断该不该通知的逻辑。判断是否发送通知应该在触发通知之前,所以判断逻辑写在 ReplyOberver 中就很好。
class ReplyObserver
{
.
.
.
public function created(Reply $reply)
{
$topic = $reply->topic;
.
.
.
if($topic->user->id !== Auth::id()){
$topic->user->notify(new TopicReplyed($reply));
}
}
}
if($topic->user->id !== Auth::id()){} 如果头部没引入Auth.这里需要改为 if($topic->user->id !== \Auth::id()){}
我喜欢这个写法