
本文详细阐述了在 Laravel 应用中如何实现数据库通知的聚合,以避免在短时间内向用户发送大量相似通知。核心策略是在特定时间窗口内,通过更新现有通知的计数和内容,而非创建新的通知,来优化用户体验。文章将深入分析 `toDatabase` 方法的机制,并提供关键代码示例,展示如何在更新操作完成后,阻止 Laravel 自动创建新的通知记录。
在现代Web应用中,通知是与用户互动的重要方式。然而,当系统在短时间内触发大量相似事件时(例如,多个新帖子在30分钟内发布),如果每个事件都生成一个独立的通知,用户可能会感到信息过载,从而降低应用的使用体验。为了解决这一问题,一种常见的策略是通知聚合:将相似的通知合并为一个,并通过更新其内容(如增加计数)来反映事件的累积。
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 方法中首先检查是否存在符合条件的近期通知。如果存在,就更新它的计数和内容;否则,创建一个新的通知。
以下是这种思路的一个初步实现:
// 初始尝试的代码片段
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
];
}
}通过在 toDatabase 方法中巧妙地运用条件判断和返回值控制,我们能够有效地实现 Laravel 数据库通知的聚合功能。当检测到在特定时间窗口内存在相似通知时,系统会更新现有通知而非创建新通知,并通过返回 null 来阻止 Laravel 的默认新通知创建行为。这一策略显著改善了用户在短时间内接收大量通知时的体验,使得通知系统更加智能和用户友好。
以上就是优化 Laravel 数据库通知:实现聚合与避免重复创建的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号