此节复写 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年前 自动加精
以构建论坛项目 larabbs 为线索,展开对 laravel 框架的全面学习。应用程序架构思路贴近 laravel 框架的设计哲学。
从零开始带你一步步开发一个 go 博客项目,让你在最短的时间内学会使用 go 进行编码。项目结构很大程度上参考了 laravel。
讨论数量: 11

找了好久的问题

6年前

good!:smile:

5年前

5.7版本中似乎有了更好的解法。

5年前

這個問題我也找了好久xd

5年前

很有用哦,点赞!!

5年前

对的,这里重写notify并不好,重新写一个通知方法里调用notify()才好

4年前

: 1:

4年前

没搞清楚这里是复现哪里的bug?

4年前

因为密码相关的都是邮件通知

public function notify($instance)
{
    // 只有数据库类型通知才需提醒,直接发送 email 或者其他的都 pass
    if (method_exists($instance, 'todatabase')) {
        // 如果要通知的人是当前用户,就不必通知了!
        if ($this->id == auth::id()) {
            return;
        }
        $this->increment('notification_count');
    }
    $this->laravelnotify($instance);
}
4年前

感觉这样写不太好 如果以后 还有其他 的通知 也用数据库方式 那这里还得改

3年前

原来这就是重新发送不执行的原因,点赞支持

知道了原因后,我的改正方法如下

// 如果要通知的人是当前用户,且不是在验证邮箱,就不必通知了!
if ($this->id == auth::id()&&get_class($instance)!="illuminate\auth\notifications\verifyemail") {
    return;
}
2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图