【发布时间】:2022-01-18 12:18:23
【问题描述】:
我在 Laravel 中构建了一个应用程序,用于向用户发送通知。
例如,当创建新帖子时,它会根据某些条件向多个用户发送通知,我面临的问题是在短时间内创建多个帖子时,例如 30 分钟我不希望用户通过大量通知获得通知, 我只想更新上次通知中的计数。
但我需要在更新最后一个通知时阻止创建新通知。
这是我写的。
public function toDatabase($notifiable): array
$count = 1;
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
]
]);
}
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
];
}
如何防止代码到达返回[]?
【问题讨论】:
标签: php laravel eloquent notifications laravel-notification