Laravel:更改通知表并插入自定义数据

时间:2021-04-30 12:26:50

标签: laravel laravel-8

我正在实施 Laravel 通知系统。我已经在 DB 中进行了插入测试,它进行得很顺利,但是在注册通知时,我看到它带有“奇怪的”数据(从我的角度来看)。

<头>
id 类型 notifiable_type notifiable_id
634db0ee-a9c0... App\Notifications\HeatMapNotification 应用\模型\用户 1

我不明白为什么“id”列带有一个 uuid 类型的 id。我的意思是,使用自动增量 ID 就足够了(我认为)。

在“类型”列中,我想插入一个更简单的文本。

我认为“notifiable_type”列没有任何用处。就我而言,应通知的实体将始终是用户。

我还想更改一些列名(notifiable_id --> user_id)

我该如何进行这些更改?

非常感谢您。

1 个答案:

答案 0 :(得分:1)

查看 DatabaseNotification 类并查看您的一些要求。例如默认有

  /**
     * The "type" of the primary key ID.
     *
     * @var string
     */
    protected $keyType = 'string';

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

也许您可以在此方面启用自动增量 ID。 在 DatabaseChannel 类中,当在通知表中存储一些数据时

protected function buildPayload($notifiable, Notification $notification)
    {
        return [
            'id' => $notification->id,
            'type' => get_class($notification),
            'data' => $this->getData($notifiable, $notification),
            'read_at' => null,
        ];
    }

我想到了类型列,它更容易转换通知类型

if ($notification->type === HeatMapNotification::class)

和notifiable_type,你可以管理很多不同的用户模型......取决于你开发的项目,所以Laravel进入这个。