【问题标题】:Laravel database notifications update count onlyLaravel 数据库通知仅更新计数
【发布时间】: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


    【解决方案1】:

    也许添加另一个条件,也许在您的数据库中添加一列 Last_send 并在执行时比较 Last_send 的当前时间,如果时间大于 30 分钟,则让它过去。

    【讨论】:

    • 我实际上是通过将代码移动到“via”方法来修复它,如果条件通过我返回空数组,否则返回['database']
    猜你喜欢
    • 2016-07-06
    • 2016-11-26
    • 1970-01-01
    • 2016-10-15
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多