【问题标题】:Laravel 5.3 - Single Notification for User Collection (followers)Laravel 5.3 - 用户收藏的单一通知(关注者)
【发布时间】:2017-05-07 13:37:21
【问题描述】:

当我有一个notifiable 用户时,会插入notifications 表中的一个条目,同时发送一个mail/sms,它可以通过渠道完美运行。

问题是当我有一个user 收藏、1k 个关注我的用户的列表时,我发布了更新。以下是在多用户情况下使用 Notifiable 特征时会发生的情况:

  1. 1k mails/sms 已发送(问题不在此处)
  2. 1k 通知条目添加到 DB 的 notifications 表中

似乎向 DB 的 notifications 表添加 1k 通知并不是最佳解决方案。由于toArray 数据是相同的,数据库的notifications 表中的所有其他内容对于1k 行都是相同的,only 区别在于notifiable_iduser @987654335 @。

开箱即用的最佳解决方案是:

  1. Laravel 会发现它是一个 array notifiable_type
  2. 单个通知保存为notifiable_type user_arrayusernotifiable_id 0(零仅用于表示它是一个多通知用户)
  3. 使用刚刚创建为 foreign_keynotification_id 创建/使用另一个表 notifications_read 并插入 1k 行,仅包含这些字段:

    notification_idnotifiable_idnotifiable_typeread_at

我希望已经有一种方法可以做到这一点,因为我现在在我的应用程序中,并且希望在这种情况下使用内置的通知和通道,因为我正在启动 emails/sms通知,我认为重复 1k 次很好,但是需要优化的是相同数据进入数据库的问题。

任何想法/想法如何在这种情况下进行?

【问题讨论】:

    标签: php laravel laravel-5 laravel-5.3


    【解决方案1】:

    2017-01-14 更新:实施更正确的方法

    简单示例:

    use Illuminate\Support\Facades\Notification;
    use App\Notifications\SomethingCoolHappen;
    
    Route::get('/step1', function () {
        // example - my followers
        $followers = App\User::all();
    
        // notify them
        Notification::send($followers, new SomethingCoolHappen(['arg1' => 1, 'arg2' => 2]));
    });
    
    Route::get('/step2', function () {
        // my follower
        $user = App\User::find(10);
    
        // check unread subnotifications
        foreach ($user->unreadSubnotifications as $subnotification) {
            var_dump($subnotification->notification->data);
            $subnotification->markAsRead();
        }
    });
    

    如何让它发挥作用?

    第 1 步 - 迁移 - 创建表(子通知)

    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateSubnotificationsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('subnotifications', function (Blueprint $table) {
                // primary key
                $table->increments('id')->primary();
    
                // notifications.id
                $table->uuid('notification_id');
    
                // notifiable_id and notifiable_type
                $table->morphs('notifiable');
    
                // follower - read_at
                $table->timestamp('read_at')->nullable();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('subnotifications');
        }
    }
    

    第 2 步 - 让我们为新的子通知表创建一个模型

    <?php
    // App\Notifications\Subnotification.php
    namespace App\Notifications;
    
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Notifications\DatabaseNotification;
    use Illuminate\Notifications\DatabaseNotificationCollection;
    
    class Subnotification extends Model
    {
        // we don't use created_at/updated_at
        public $timestamps = false;
    
        // nothing guarded - mass assigment allowed
        protected $guarded = [];
    
        // cast read_at as datetime
        protected $casts = [
            'read_at' => 'datetime',
        ];
    
        // set up relation to the parent notification
        public function notification()
        {
            return $this->belongsTo(DatabaseNotification::class);
        }
    
        /**
         * Get the notifiable entity that the notification belongs to.
         */
        public function notifiable()
        {
            return $this->morphTo();
        }
    
        /**
         * Mark the subnotification as read.
         *
         * @return void
         */
        public function markAsRead()
        {
            if (is_null($this->read_at)) {
                $this->forceFill(['read_at' => $this->freshTimestamp()])->save();
            }
        }
    }
    

    第 3 步 - 创建自定义数据库通知通道
    更新:使用静态变量 $map 保留第一个通知 id 并插入下一个通知(使用相同的数据) 而不在notifications 表中创建记录

    <?php
    // App\Channels\SubnotificationsChannel.php
    namespace App\Channels;
    
    use Illuminate\Notifications\DatabaseNotification;
    use Illuminate\Notifications\Notification;
    
    class SubnotificationsChannel
    {
        /**
         * Send the given notification.
         *
         * @param  mixed                                  $notifiable
         * @param  \Illuminate\Notifications\Notification $notification
         *
         * @return void
         */
        public function send($notifiable, Notification $notification)
        {
            static $map = [];
    
            $notificationId = $notification->id;
    
            // get notification data
            $data = $this->getData($notifiable, $notification);
    
            // calculate hash
            $hash = md5(json_encode($data));
    
            // if hash is not in map - create parent notification record
            if (!isset($map[$hash])) {
                // create original notification record with empty notifiable_id
                DatabaseNotification::create([
                    'id'              => $notificationId,
                    'type'            => get_class($notification),
                    'notifiable_id'   => 0,
                    'notifiable_type' => get_class($notifiable),
                    'data'            => $data,
                    'read_at'         => null,
                ]);
    
                $map[$hash] = $notificationId;
            } else {
                // otherwise use another/first notification id
                $notificationId = $map[$hash];
            }
    
            // create subnotification
            $notifiable->subnotifications()->create([
                'notification_id' => $notificationId,
                'read_at'         => null
            ]);
        }
    
        /**
         * Prepares data
         *
         * @param mixed                                  $notifiable
         * @param \Illuminate\Notifications\Notification $notification
         *
         * @return mixed
         */
        public function getData($notifiable, Notification $notification)
        {
            return $notification->toArray($notifiable);
        }
    }
    

    第 4 步 - 创建通知
    更新:现在通知支持所有渠道,而不仅仅是子通知

    <?php
    // App\Notifications\SomethingCoolHappen.php
    namespace App\Notifications;
    
    use App\Channels\SubnotificationsChannel;
    use Illuminate\Bus\Queueable;
    use Illuminate\Notifications\Notification;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Notifications\Messages\MailMessage;
    
    class SomethingCoolHappen extends Notification
    {
        use Queueable;
    
        protected $data;
    
        /**
         * Create a new notification instance.
         *
         * @return void
         */
        public function __construct($data)
        {
            $this->data = $data;
        }
    
        /**
         * Get the notification's delivery channels.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function via($notifiable)
        {
            /**
             * THIS IS A GOOD PLACE FOR DETERMINING NECESSARY CHANNELS
             */
            $via = [];
            $via[] = SubnotificationsChannel::class;
            //$via[] = 'mail';
            return $via;
        }
    
        /**
         * Get the mail representation of the notification.
         *
         * @param  mixed  $notifiable
         * @return \Illuminate\Notifications\Messages\MailMessage
         */
        public function toMail($notifiable)
        {
            return (new MailMessage)
                        ->line('The introduction to the notification.')
                        ->action('Notification Action', 'https://laravel.com')
                        ->line('Thank you for using our application!');
        }
    
        /**
         * Get the array representation of the notification.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function toArray($notifiable)
        {
            return $this->data;
        }
    }
    

    第 5 步 - “追随者”的辅助特征

    <?php
    // App\Notifications\HasSubnotifications.php
    namespace App\Notifications;
    
    trait HasSubnotifications
    {
        /**
         * Get the entity's notifications.
         */
        public function Subnotifications()
        {
            return $this->morphMany(Subnotification::class, 'notifiable')
                ->orderBy('id', 'desc');
        }
    
        /**
         * Get the entity's read notifications.
         */
        public function readSubnotifications()
        {
            return $this->Subnotifications()
                ->whereNotNull('read_at');
        }
    
        /**
         * Get the entity's unread notifications.
         */
        public function unreadSubnotifications()
        {
            return $this->Subnotifications()
                ->whereNull('read_at');
        }
    }
    

    第 6 步 - 更新您的用户模型
    更新:不需要 followers 方法

    namespace App;
    
    use App\Notifications\HasSubnotifications;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    
    class User extends Authenticatable
    {
        use Notifiable;
    
        /**
         * Adding helpers to followers:
         *
         * $user->subnotifications - all subnotifications
         * $user->unreadSubnotifications - all unread subnotifications
         * $user->readSubnotifications - all read subnotifications
         */
        use HasSubnotifications;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'name', 'email', 'password',
        ];
    
        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 'remember_token',
        ];
    }
    

    【讨论】:

    • 谢谢!这真的很好:) 在第 4 步中,我注意到 toMail 丢失了,我有一些逻辑可以找到想要电子邮件的追随者,但是我该把逻辑放在哪里呢?由于该示例使用$user-&gt;notify(new FollowersUpdate(...,因此当我在步骤4 中输入toMail 时,它会通过电子邮件发送$user,这不是我们想要的,因为我们想使用一些用户详细信息向关注者发送电子邮件。我不认为把它放在FollowerChannel 是正确的,因为它处理数据库表条目?知道实施的正确位置是什么吗?
    • @Wonka,正如你之前所做的那样。此方法仅实现自定义数据库通道。您可以尝试将您的逻辑放在 FollowerChannel 中,但您是对的 - 这不是一个好地方。换句话说,您必须同时使用Notifications:带有短信/电子邮件的原始通知和FollowersUpdate(进行子通知)。我相信这对于您的优化来说已经足够小了。
    • @Wonka,我想了更多,并提出了更正确的解决方案。检查。希望你会发现它更有用。
    • 谢谢 :) 我使用了您更新的示例,它允许在一个通知中使用多个频道,因此对于 100 个关注者,它会添加 100 个子通知,但它也会发送 100 封电子邮件,即使 100 个中只有 80 个想要电子邮件,其他 20 人也收到电子邮件。我打开了这个问题:stackoverflow.com/questions/41643939/… 但提供的解决方案是为每个$follower 找出他们的偏好并循环通知,而不是发送$followers 并处理频道中的首选项。
    • 是否可以像在您的示例中一样拥有与传递的$followers 相同的单个通知文件,但也尊重关注者的邮件首选项?我试过但无法让它工作......
    【解决方案2】:

    是的,我猜你是对的,使用默认的 Notifiable 特征,你可以创建一个 custom channel

    您可以检查Illuminate\Notifications\Channels\DatabaseChannel 类的默认创建并将其应用于数据透视表。

    希望这有助于创建带有数据透视表的新频道。此外,为您自己的 Notifiable 特征实现 HasDatabasePivotNotifications 特征(或类似名称)。

    【讨论】:

    • 您介意分享具体代码来展示如何实现吗?
    • 我加了 100 分赏金,因为我不是 100% 确定如何实施您的解决方案。你能用示例代码/伪代码充实你的解决方案,以便我可以实现它并报告结果吗?很多人似乎对此很感兴趣:)
    • 我会添加到周日(从现在起 3 天)
    • @Isfettcom 你还能展示一下实现吗?
    猜你喜欢
    • 2018-09-08
    • 2018-04-25
    • 1970-01-01
    • 2017-05-29
    • 2017-10-11
    • 2017-04-08
    • 2017-04-27
    • 2015-03-06
    • 2016-11-23
    相关资源
    最近更新 更多