优化 Laravel 数据库通知:实现聚合与避免重复创建

霞舞
发布: 2025-11-23 12:27:01
原创
187人浏览过

优化 laravel 数据库通知:实现聚合与避免重复创建

本文详细阐述了在 Laravel 应用中如何实现数据库通知的聚合,以避免在短时间内向用户发送大量相似通知。核心策略是在特定时间窗口内,通过更新现有通知的计数和内容,而非创建新的通知,来优化用户体验。文章将深入分析 `toDatabase` 方法的机制,并提供关键代码示例,展示如何在更新操作完成后,阻止 Laravel 自动创建新的通知记录。

引言:优化用户通知体验

在现代Web应用中,通知是与用户互动的重要方式。然而,当系统在短时间内触发大量相似事件时(例如,多个新帖子在30分钟内发布),如果每个事件都生成一个独立的通知,用户可能会感到信息过载,从而降低应用的使用体验。为了解决这一问题,一种常见的策略是通知聚合:将相似的通知合并为一个,并通过更新其内容(如增加计数)来反映事件的累积。

Laravel 数据库通知机制概述

Laravel 提供了强大的通知系统,其中数据库通知允许我们将通知存储在数据库中。当使用 Illuminate\Notifications\Notification 类时,我们需要实现 toDatabase 方法来定义通知的数据结构。这个方法返回一个数组,Laravel 会将这个数组序列化并存储在 notifications 表的 data 字段中。

例如,一个基本的 toDatabase 方法可能如下所示:

public function toDatabase($notifiable): array
{
    return [
        'content' => [
            'en' => "1 new post matched with your saved search {$this->search->title} has been posted, Press here to view more.",
        ],
        'count' => 1,
        'search' => $this->search->id,
        'parameters' => $this->search->parameters
    ];
}
登录后复制

当 Notification::send() 或 Notifiable::notify() 被调用时,Laravel 会执行这个 toDatabase 方法,并将其返回的数据保存为新的通知记录。

现有尝试与核心挑战

为了实现通知聚合,一个直观的想法是在 toDatabase 方法中首先检查是否存在符合条件的近期通知。如果存在,就更新它的计数和内容;否则,创建一个新的通知。

以下是这种思路的一个初步实现:

微撰
微撰

AI智能写作平台

微撰 207
查看详情 微撰
// 初始尝试的代码片段
public function toDatabase($notifiable): array
{
    $count = 1;
    // 尝试查找在过去30分钟内,与当前搜索相关的现有通知
    if ($notification = $notifiable->notifications()
        ->where('data->search', $this->search->id)
        ->where('updated_at', '>=', now()->subMinutes(30))
        ->first()) {

        // 如果找到,更新其计数
        $count = isset($notification->data['count']) ? $notification->data['count'] + 1 : 1;
        $notification->update([
            'data' => [
                'content' => [
                    'en' => "{$count} new posts matched with your saved search {$this->search->title} has been posted, Press here to view more.",
                ],
                'count' => $count,
                // 确保保留其他原有数据
                'search' => $this->search->id,
                'parameters' => $this->search->parameters
            ]
        ]);
    }

    // 问题所在:无论是否更新了现有通知,这里都会返回一个数组
    return [
        'content' => [
            'en' => "{$count} new post matched with your saved search {$this->search->title} has been posted, Press here to view more.",
        ],
        'count' => $count,
        'search' => $this->search->id,
        'parameters' => $this->search->parameters
    ];
}
登录后复制

上述代码的核心挑战在于,即使成功更新了现有的通知,toDatabase 方法的最后依然会 return []。根据 Laravel 的通知发送机制,只要 toDatabase 方法返回一个非空数组,Laravel 就会将其视为一个新的通知数据,并将其保存到数据库中。这就导致了即使我们更新了旧通知,系统仍然会创建一个新的通知,从而无法达到聚合的目的。

解决方案:条件性阻止新通知创建

解决这个问题的关键在于,当成功更新现有通知时,阻止 toDatabase 方法返回一个有效的通知数据数组。Laravel 的 DatabaseChannel 在处理 toDatabase 的返回值时,会检查其是否为 null 或空数组。如果返回 null 或空数组,则不会创建新的通知记录。

因此,我们可以在更新现有通知后,直接返回 null 或一个空数组 [],以告知 Laravel 不需要创建新的通知。

以下是优化后的 toDatabase 方法实现:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\DatabaseMessage; // 如果需要自定义DatabaseMessage
use App\Models\Search; // 假设您的Search模型

class NewPostsMatchedSearch extends Notification // 假设这是您的通知类
{
    use Queueable;

    protected $search;

    /**
     * 创建一个新的通知实例。
     *
     * @param Search $search
     * @return void
     */
    public function __construct(Search $search)
    {
        $this->search = $search;
    }

    /**
     * 获取通知的交付渠道。
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database'];
    }

    /**
     * 获取通知的数据库表示。
     *
     * @param  mixed  $notifiable
     * @return array|null // 关键:返回类型可以是 array 或 null
     */
    public function toDatabase($notifiable): ?array
    {
        // 1. 定义聚合的时间窗口(例如30分钟)
        $timeWindow = now()->subMinutes(30);

        // 2. 尝试查找在指定时间窗口内,针对当前用户和当前搜索的同类型通知
        // 务必添加 'type' 过滤,以确保只聚合相同类型的通知
        $existingNotification = $notifiable->notifications()
            ->where('type', self::class) // 过滤通知类型
            ->where('data->search', $this->search->id) // 过滤搜索ID
            ->where('updated_at', '>=', $timeWindow) // 过滤时间窗口
            ->first();

        if ($existingNotification) {
            // 3. 如果找到现有通知,则更新其计数和内容
            $newCount = isset($existingNotification->data['count']) ? $existingNotification->data['count'] + 1 : 1;
            $existingNotification->update([
                'data' => [
                    'content' => [
                        'en' => "{$newCount} new posts matched with your saved search {$this->search->title} has been posted, Press here to view more.",
                    ],
                    'count' => $newCount,
                    'search' => $this->search->id,
                    'parameters' => $this->search->parameters,
                ]
            ]);

            // 4. 关键步骤:返回 null 或空数组以阻止 Laravel 创建新的通知记录
            return null; // 或者 return [];
        }

        // 5. 如果没有找到现有通知,则创建新的通知
        return [
            'content' => [
                'en' => "1 new post matched with your saved search {$this->search->title} has been posted, Press here to view more.",
            ],
            'count' => 1,
            'search' => $this->search->id,
            'parameters' => $this->search->parameters
        ];
    }
}
登录后复制

注意事项与最佳实践

  1. 通知类型过滤 (where('type', self::class)): 在查询现有通知时,务必通过 type 字段进行过滤。type 字段存储了通知类的完整命名空间,这确保了我们只聚合相同类型的通知,避免将不同类型的通知错误地合并。
  2. updated_at 字段的利用: Laravel 数据库通知表自带 updated_at 字段,它会在每次记录更新时自动更新。这使得它非常适合作为判断通知是否在特定时间窗口内的依据。
  3. 数据结构的一致性: 在更新 data 字段时,要确保其结构与首次创建时一致。如果只更新 count 而丢失了 search 或 parameters 等其他重要信息,可能会导致通知显示不完整或功能异常。因此,在更新时应确保所有相关数据都被重新包含在 data 数组中。
  4. 可读性与维护性: 尽管在 toDatabase 方法中包含复杂的逻辑是可行的,但对于更复杂的聚合需求,可以考虑将通知查找和更新逻辑封装到单独的服务类或仓库中,以提高代码的可读性和可维护性。
  5. 前端展示: 当通知被聚合后,前端需要能够正确解析 data 字段中的 count 和 content,以展示聚合后的信息。

总结

通过在 toDatabase 方法中巧妙地运用条件判断和返回值控制,我们能够有效地实现 Laravel 数据库通知的聚合功能。当检测到在特定时间窗口内存在相似通知时,系统会更新现有通知而非创建新通知,并通过返回 null 来阻止 Laravel 的默认新通知创建行为。这一策略显著改善了用户在短时间内接收大量通知时的体验,使得通知系统更加智能和用户友好。

以上就是优化 Laravel 数据库通知:实现聚合与避免重复创建的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号