首页 > 解决方案 > 排队时 Laravel 通知不起作用

问题描述

当用户添加新注释时,我正在使用通知向管理员发送电子邮件。如果我不使用 ShouldQueue,一切正常。当我使用队列时出现错误

ErrorException:未定义的属性:App\Notifications\NewKidNote::$note

可能是什么原因?这是我的通知代码

<?php

namespace App\Notifications;

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

class NewKidNote extends Notification
{
    use Queueable;

    protected $note;
    protected $kidname;
    protected $userfullname;
    protected $color;
    protected $sentnote;
    protected $kidid;

    public function __construct($note)
    {
        $this->note          = $note;
        $this->kidname       = $note->kid->name;
        $this->userfullname  = $note->user->fullname;
        $this->color         = $note->color;
        $this->sentnote      = $note->note;
        $this->kidid         = $note->kid_id;
    }

    public function via($notifiable)
    {
        return ['mail','database'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
              ->subject('New Note added')
              ->greeting('Hello ')
              ->line('New Note has been added for '. $this->kidname. ' by '.$this->userfullname)
              ->line('Note Color: '.$this->color)
              ->line('Note')
              ->line($this->sentnote)
              ->action('You can also see the note here', route('admin.children.show',$this->kidid));
    }
    
    public function toDatabase($notifiable)
    {
        return [
           'icon'    => 'notepad',
           'color'   => $this->color,
           'message' => 'New note added for '. $this->kidname ,
           'link'    => route('admin.children.show',$this->kidid)
        ];
    }
}

标签: laravelqueue

解决方案


自我注意..确保清理缓存并重新启动队列:))然后它工作正常!!这段代码运行完美。谢谢


推荐阅读