使用队列发送邮件🤔️

事情是这样子的,我按照教程使用队列发送邮件,代码如下

<?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 implements ShouldQueue
{
    use Queueable;

    public $reply;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(Reply $reply)
    {
        // 注入回复实体,方便 toDatabase 方法中的使用
        $this->reply = $reply;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        // 开启通知的频道
        return ['database', 'mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $url = $this->reply->topic->link(['#reply' . $this->reply->id]);

        return (new MailMessage)
            ->line('你的话题有新回复!')
            ->action('查看回复', $url);
    }

    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,
        ];
    }

然后回复成功,邮件没发出去,在horizon监控到fail

Error

ErrorException: Undefined property: Illuminate\Contracts\Database\ModelIdentifier::$topic in /home/vagrant/Code/larabbs/app/Notifications/TopicReplied.php:46

这时候Google Illuminate\Contracts\Database\ModelIdentifier
stackoverflow上面大概看了下,没解决。
然后重新仔细看了遍这章教程,把toMail方法toDatabase方法位置替换了下,再重新跑了一遍,成功收到邮件通知,这就是我疑惑之处

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 1

未知的属性$topic,应该是$reply没有传过来

5年前 评论

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