相关疑难解决方法(0)

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

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

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

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

我想添加列event_id,question_id,user_id(在创建该通知用户)等等为默认laravel notifications

如何覆盖send函数以包含更多列?

在:

vendor/laravel/framework/src/Illuminate/Notifications/Channels/DatabaseChannel.php
Run Code Online (Sandbox Code Playgroud)

代码:

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' ? …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-5 laravel-6

9
推荐指数
3
解决办法
7930
查看次数

标签 统计

laravel ×1

laravel-5 ×1

laravel-6 ×1