分析下消息通知发送流程
评论完成触发观察方法中 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() 这句看懂,怎么就就取出 所以的消息通知了呢?
推荐文章: