Laravel:自定义或扩展通知 - 数据库模型

标签 laravel laravel-5 laravel-6

恕我直言,当前用于在 Laravel 中保存通知的数据库 channel 设计非常糟糕:

  • 您不能在项目上使用外键级联来清理已删除项目的通知,例如
  • data 中搜索自定义属性列(转换为数组)不是最佳的

  • 您将如何扩展 DatabaseNotification供应商包中的模型?

    我想添加列 event_id , question_id , user_id (创建通知的用户)等...到默认 Laravel notifications table

    你如何覆盖send功能以包含更多列?

    在:
    vendor/laravel/framework/src/Illuminate/Notifications/Channels/DatabaseChannel.php
    

    编码:
    class DatabaseChannel
    {
     /**
      * Send the given notification.
      *
      * @param  mixed  $notifiable
      * @param  \Illuminate\Notifications\Notification  $notification
      * @return \Illuminate\Database\Eloquent\Model
      */
     public function send($notifiable, Notification $notification)
     {
        return $notifiable->routeNotificationFor('database')->create([
            'id' => $notification->id,
            'type' => get_class($notification),
    
          \\I want to add these
            'user_id' => \Auth::user()->id,
            'event_id' => $notification->type =='event' ? $notification->id : null, 
            'question_id' => $notification->type =='question' ? $notification->id : null,
          \\End adding new columns
    
            'data' => $this->getData($notifiable, $notification),
            'read_at' => null,
        ]);
     }
    }
    

    最佳答案

    创建自定义通知 channel :

    首先,在 App\Notifications 中创建一个类,例如:

    <?php
    
    namespace App\Notifications;
    
    use Illuminate\Notifications\Notification;
    
    class CustomDbChannel 
    {
    
      public function send($notifiable, Notification $notification)
      {
        $data = $notification->toDatabase($notifiable);
    
        return $notifiable->routeNotificationFor('database')->create([
            'id' => $notification->id,
    
            //customize here
            'answer_id' => $data['answer_id'], //<-- comes from toDatabase() Method below
            'user_id'=> \Auth::user()->id,
    
            'type' => get_class($notification),
            'data' => $data,
            'read_at' => null,
        ]);
      }
    
    }
    

    二、在via中使用这个 channel 通知类中的方法:

    <?php
    
    namespace App\Notifications;
    
    use Illuminate\Notifications\Notification;
    
    use App\Notifications\CustomDbChannel;
    
    class NewAnswerPosted extends Notification
    {
      private $answer;
    
      public function __construct($answer)
      {
        $this->answer = $answer;
      }
    
      public function via($notifiable)
      {
        return [CustomDbChannel::class]; //<-- important custom Channel defined here
      }
    
      public function toDatabase($notifiable)
      {
        return [
          'type' => 'some data',
          'title' => 'other data',
          'url' => 'other data',
          'answer_id' => $this->answer->id //<-- send the id here
        ];
      }
    }
    

    关于Laravel:自定义或扩展通知 - 数据库模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43646085/

    相关文章:

    php - Laravel Sail 部署就绪了吗

    php - Laravel 电子邮件验证链接不起作用?

    php - 在 Laravel Eloquent 模型中创建动态命名的修改器

    laravel-5 - 我可以直接将 Laravel 5.5 升级到 Laravel 6 吗?

    Laravel 资源分页元数据丢失

    laravel - 如何在已编译的 View 文件中执行 javascript(使用 View::render 方法)

    php - Laravel:+100 表......是否使用迁移?

    php - 从 Lumen/Laravel 中的路由中删除 index.php

    laravel - 电话号码是整数还是字符串?

    php - Laravel 为 hasManyThrough 关系调用未定义的方法 Illuminate\Database\Query\Builder::detach/attach()