You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何定制Laravel默认通知架构及通知表自定义字段(无需重复代码)

定制Laravel默认Database通知通道及扩展通知表结构

Laravel默认notifications表结构

idtypenotifiable_typenotifiable_iddataread_atcreated_atupdated_at
fd71b94e-a0fc-42d5-9cfc-d0019cd83468Notifications\PushNotificationUser23{"user_name":"Bak thonas","texts":"testing"}null2023-01-09 12:43:022023-01-09 12:43:02

扩展后的notifications表结构

idtypenotifiable_typenotifiable_idrelation_typerelation_idsubject_ensubject_armessage_enmessage_ardataread_atcreated_atupdated_at
fd71b94e-a0fc-42d5-9cfc-d0019cd83468Notifications\PushNotificationUser23B22WelcomeمرحباWelcome to platform!مرحبا بكم في Platform!{"user_name":"Bak thonas","texts":"testing"}null2023-01-09 12:43:022023-01-09 12:43:02

定制Database通知通道(仅修改数据构建逻辑)

1. 自定义Database通道类

直接继承Laravel原生的DatabaseChannel,只重写负责构建数据库存储数据的buildPayload方法,不用重复其他核心逻辑:

<?php

namespace App\Notifications\Channels;

use Illuminate\Notifications\Channels\DatabaseChannel as BaseDatabaseChannel;
use Illuminate\Notifications\Notification;

class DatabaseChannel extends BaseDatabaseChannel
{
    protected function buildPayload($notifiable, Notification $notification)
    {
        // 先获取父类生成的默认数据结构
        $payload = parent::buildPayload($notifiable, $notification);
        
        // 从通知实例中读取自定义字段数据
        $customFields = $notification->toDatabaseCustom($notifiable);
        
        // 合并默认数据与自定义字段,返回最终存储结构
        return array_merge($payload, $customFields);
    }
}

2. 修改通知类,添加自定义字段方法

在你的通知类(比如PushNotification)中新增toDatabaseCustom方法,返回需要存入自定义字段的内容,同时指定使用我们自定义的通道:

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;

class PushNotification extends Notification
{
    private $relationType;
    private $relationId;
    private $subjectEn;
    private $subjectAr;
    private $messageEn;
    private $messageAr;
    private $data;

    public function __construct($relationType, $relationId, $subjectEn, $subjectAr, $messageEn, $messageAr, $data)
    {
        $this->relationType = $relationType;
        $this->relationId = $relationId;
        $this->subjectEn = $subjectEn;
        $this->subjectAr = $subjectAr;
        $this->messageEn = $messageEn;
        $this->messageAr = $messageAr;
        $this->data = $data;
    }

    // 保留原生toDatabase方法,生成默认data字段内容
    public function toDatabase($notifiable)
    {
        return $this->data;
    }

    // 新增方法,返回自定义字段的键值对
    public function toDatabaseCustom($notifiable)
    {
        return [
            'relation_type' => $this->relationType,
            'relation_id' => $this->relationId,
            'subject_en' => $this->subjectEn,
            'subject_ar' => $this->subjectAr,
            'message_en' => $this->messageEn,
            'message_ar' => $this->messageAr,
        ];
    }

    // 指定使用我们自定义的Database通道
    public function via($notifiable)
    {
        return [\App\Notifications\Channels\DatabaseChannel::class];
    }
}

3. 发送通知

调用通知时传入对应的参数即可:

$user->notify(new \App\Notifications\PushNotification(
    'B',
    22,
    'Welcome',
    'مرحبا',
    'Welcome to platform!',
    'مرحبا بكم في Platform!',
    ['user_name' => 'Bak thonas', 'texts' => 'testing']
));

这样就实现了在完全复用Laravel原生数据库通知逻辑的基础上,仅扩展自定义字段的存储需求,无需重复编写已有的核心代码。

内容的提问来源于stack exchange,提问作者M-Khawar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 11:20:55