分析下消息通知发送流程

评论完成触发观察方法中 ReplyOberserve 中的 created 方法

public function created(Reply $reply)
    {
        $topic = $reply->topic;
        $reply->topic->increment('reply_count', 1);
        $topic->user->notify(new TopicReplied($reply));
    }

$topic = $reply->topic 这个在Reply模型中写好的一对一

public function topic()
    {
        return $this->belongsTo(Topic::class);
    }

$topic->user->notify(new TopicReplied($reply));

$topic->user  这个是在Topic模型中写好的一对一 ($user Model)

public function user()
    {
        return $this->belongsTo(User::class);
    }

在User模型里有调用了 notify() 方法
public function notify($instance)
    {
        if($this->id == Auth::id()) {
            return;
        }

        $this->increment('notification_count');
        $this->laravelNotify($instance);
    }
    notify($instance) 这个方法 需要注册一个实例
    use Notifiable {
            notify as protected LaravelNotify;
     }

     因为 $topic->user->notify(new TopicReplied($reply)); notify 和 User 模型中notify 重复了 所以这个notify 重命名
 我主要的 这个在 notificationControlller.php 
 中的  $notifications = Auth::user()->notifications()   这句看懂,怎么就就取出 所以的消息通知了呢?
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 4

我觉得思路有段乱,有不对的地方,麻烦指出,谢谢啦。

5年前 评论
ruodee
notifications()方法是HasDatabaseNotifications Trait中定义的一个方法,
Models/User 中引入了 Notifiable Trait,
Notifiable Traint引入了HasDatabaseNotifications Trait。

方法定义: public function notifications() { return $this->morphMany(DatabaseNotification::class, 'notifiable') ->orderBy('created_at', 'desc'); } 取出所有通知信息。
5年前 评论

@老好人 谢谢,这样我就理解了

5年前 评论
pardon110

notifications 是一个多态关联方法

4年前 评论

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