此节复写 trait 中的 notify 方法,会导致框架自带的「重新发送邮件」的功能失效,建议在 user model 类中新增一个 topicNofity 方法单独处理即可。
此节重写了 \Illuminate\Notifications\RoutesNotifications::notify
,导致如果是发送给自己的通知,就不会发送 $this->id == Auth::id()
,但是激活邮件是需要发送给自己的。所以,最好是不要重写trait中的notify
方法,而改为在user model类中新增一个topicNotify()
的方法,代码如下:
-
app/Models/User.php
// public function notify($instance) // { // // 如果要通知的人是当前用户,就不必通知了! // if ($this->id == Auth::id()) { // return; // } // $this->increment('notification_count'); // $this->laravelNotify($instance); // } public function topicNotify($instance) { // 如果要通知的人是当前用户,就不必通知了! if ($this->id == Auth::id()) { return; } $this->increment('notification_count'); $this->notify($instance); }
- app/Observers/ReplyObserver.php
public function created(Reply $reply) { $topic = $reply->topic; $reply->topic->increment('reply_count', 1); // 通知作者话题被回复了 $topic->user->topicNotify(new TopicReplied($reply)); }
- 测试「重新发送邮件」和「消息通知」的功能,成功!
本帖已被设为精华帖!
本帖由系统于 5年前 自动加精
推荐文章: