使用队列发送邮件🤔️

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

<?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方法位置替换了下,再重新跑了一遍,成功收到邮件通知,这就是我疑惑之处

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 1

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

5年前 评论

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