为什么发了两次邮件?

代码如下, 每次回复 发送了两次邮件

[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);
    }
}
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

一个是站内通知,一个是邮件通知

6年前 评论
讨论数量: 3

一个是站内通知,一个是邮件通知

6年前 评论

我这边的代码试过正常收到一封邮件。

ReplyObserver.php

<?php

namespace App\Observers;

use App\Models\Reply;

// creating, created, updating, updated, saving,
// saved,  deleting, deleted, restoring, restored

use App\Notifications\TopicReplied;

class ReplyObserver
{
    public function creating(Reply $reply)
    {
        $reply->content = clean($reply->content, 'user_topic_body');
    }

    public function updating(Reply $reply)
    {
        //
    }

    public function created(Reply $reply)
    {
        $topic = $reply->topic;
        $topic->increment('reply_count', 1);

        // 通知作者话题被回复了
        $topic->user->notify(new TopicReplied($reply));
    }

    public function deleted(Reply $reply)
    {
        $reply->topic->decrement('reply_count', 1);
    }
}

TopicReplied.php

<?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;

    public function __construct(Reply $reply)
    {
        // 注入回复实体,方便 toDatabase 方法中的使用
        $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);
    }
}
6年前 评论
洛未必达

hello,我在 TopicReplied.php 文件中试着开启了两次 mail 频道,于是就出现了你说的那种发送两次邮件的情况,所以我猜你的原因是这个。

.
.
public function via($notifiable)
    {
        // 开启通知的频道
        return ['mail','mail'];
    }
.
.

file

6年前 评论

一个是站内通知,一个是邮件通知

6年前 评论

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