如何理解通知类里面的内容

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\Models\Reply;

class TopicReplied extends Notification
{
    use Queueable;

    public $reply;

    public function __construct(Reply $reply)
    {
        // 注入回复实体,方便 toDatabase 方法中的使用
        $this->reply = $reply;
    }

    public function via($notifiable)
    {
        // 开启通知的频道
        return ['database'];
    }

    public function toDatabase($notifiable)
    {
        $topic = $this->reply->topic;
        $link =  $topic->link(['#reply' . $this->reply->id]);

        // 存入数据库里的数据
        return [
            'reply_id' => $this->reply->id,
            'reply_content' => $this->reply->content,
            'user_id' => $this->reply->user->id,
            'user_name' => $this->reply->user->name,
            'user_avatar' => $this->reply->user->avatar,
            'topic_link' => $link,
            'topic_id' => $topic->id,
            'topic_title' => $topic->title,
        ];
    }
}

问题1:public $reply;
问题2:// 注入回复实体,方便 toDatabase 方法中的使用
$this->reply = $reply;

    如何理解呢
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 1
Epona
  1. 学一下PHP的基础知识。
  2. $this->reply相当于你在其他地方(比如控制器中)调用这个通知时传入的reply。 如
class XxxController extends Controller
{
    public function store()
    {
        $reply = Reply::create(request()->xxxx());

        $user = 你想要通知的User;

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

这样你的通知类中的$this->reply 就相当于刚才创建的那个reply了。

5年前 评论
李小明 (楼主) 5年前
Epona (作者) 5年前
qinplain 5年前
lujiancai 5年前
李小明 (楼主) 5年前
李小明 (楼主) 5年前
lujiancai 5年前
lujiancai 5年前

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