gpt4 book ai didi

php - 如何在临近到期时间时向用户发送通知(每年)

转载 作者:行者123 更新时间:2023-11-29 17:15:50 25 4
gpt4 key购买 nike

我有一个发票模型。我尝试在临近到期时间时向用户发送通知,但我不知道它是如何实现的。

我的 table :

| service_name | start_date(date) | notification_at(date) |
|:-------------|-----------------:|:---------------------:|
| example | 2018-08-01 | NULL |

如果指定的 start_date 设置为日期,我想在每年之前 30 天向用户发送通知。因此,当现在是 2019-07-1 时发送通知。我如何在 Laravel 中使用 schedule 检查每天?

我尝试过的情况:

发票::whereNotNull('start_date')->whereDate('start_date','....');

最佳答案

已解决

其实我想做的场景就是这些。例如,

  • 首次发送通知的日期应该是2019-07-01(每年前30天)

  • 应该是第二次发送通知的日期是2019-07-15(每年的前15天)

  • 应该是第三次发送通知的日期是2019-07-26(每年的前5天)

  • 明年应该重复这些步骤

我找到了这样的解决方案。首先,我确实像这样修改了我的表。

| service_name | start_date(date) | next_notification_date(date) | next_add_day(int) |
|:-------------|-----------------:|:----------------------------:|:-----------------:|
| example | 2018-08-01 | NULL | NULL |

我为该进程创建了一个命令类。

ExpirationCommand.php

public function handle()
{
$now = Carbon::now();

Invoice::whereNotNull('start_date')->whereDate('next_notification_date', $now->format('Y-m-d'))->each(function ($invoice) use ($now) {
// defines
$addDay = $invoice->next_add_day;
$addYear = 365 - 30 - 15 - 5;

// send notification
event(new ExpirationInvoiceEvent($invoice->project,$invoice->next_notification_date->diffInDays($now)));

// setup db
if ( ! $addDay || $addDay === $addYear) {
$addDay = 30;
} elseif ($addDay !== 15 && $addDay !== 5) {
$addDay = 15;
} elseif ($addDay !== 5) {
$addDay = 5;
} else {
$addDay = $addYear;
}

$invoice->update([
'next_notification_date' => $now->addDay($addDay)->format('Y-m-d'),
'next_add_day' => $addDay,
]);
});
}

最后,我在 Console\Kernel.php 中添加命令以进行一天两次的控制

$schedule->command('expiration-control')->twiceDaily();

关于php - 如何在临近到期时间时向用户发送通知(每年),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51632651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com