此节复写 trait 中的 notify 方法,会导致框架自带的「重新发送邮件」的功能失效,建议在 user model 类中新增一个 topicnofity 方法单独处理即可。 | laravel | laravel china 社区-江南app体育官方入口
此节重写了 \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年前 自动加精
找了好久的问题
good!:smile:
5.7版本中似乎有了更好的解法。
這個問題我也找了好久xd
很有用哦,点赞!!
对的,这里重写notify并不好,重新写一个通知方法里调用notify()才好
: 1:
没搞清楚这里是复现哪里的bug?
因为密码相关的都是邮件通知
感觉这样写不太好 如果以后 还有其他 的通知 也用数据库方式 那这里还得改
原来这就是重新发送不执行的原因,点赞支持
知道了原因后,我的改正方法如下