使用队列发送邮件🤔️
事情是这样子的,我按照教程使用队列发送邮件,代码如下
<?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方法位置替换了下,再重新跑了一遍,成功收到邮件通知,这就是我疑惑之处
推荐文章: