为什么发了两次邮件?
代码如下, 每次回复 发送了两次邮件
[2018-03-06 10:02:36] Processing: App\Notifications\TopicReplied
[2018-03-06 10:02:36] Processing: App\Notifications\TopicReplied
[2018-03-06 10:02:40] Processed: App\Notifications\TopicReplied
[2018-03-06 10:02:49] Processed: App\Notifications\TopicReplied
[2018-03-06 10:06:53] Processing: App\Notifications\TopicReplied
[2018-03-06 10:06:53] Processed: App\Notifications\TopicReplied
[2018-03-06 10:06:53] Processing: App\Notifications\TopicReplied
[2018-03-06 10:06:56] Processed: App\Notifications\TopicReplied
ReplyObserver.php
class ReplyObserver
{
public function created(Reply $reply)
{
$topic = $reply->topic;
$topic->increment('reply_count', 1);
// 通知作者话题被回复了
$topic->user->notify(new TopicReplied($reply));
}
public function creating(Reply $reply)
{
$reply->topic->increment('reply_count', 1);
$reply->content = clean($reply->content, 'user_topic_body');
}
}
TopicReplied.php
class TopicReplied extends Notification implements ShouldQueue
{
use Queueable;
public $reply;
public function __construct(Reply $reply)
{
$this->reply = $reply;
}
public function via($notifiable)
{
return ['database', 'mail'];
}
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,
];
}
public function toMail($notifiable)
{
$url = $this->reply->topic->link(['#reply' . $this->reply->id]);
return (new MailMessage)
->line('你的话题有新回复!')
->action('查看回复', $url);
}
}
推荐文章: