I have an invoice model. I trying if approached expiration time send notification to users but I don't know how does it.
My table:
| service_name | start_date(date) | notification_at(date) |
|:-------------|-----------------:|:---------------------:|
| example | 2018-08-01 | NULL |
I want to 30 days before every year send notification to user if specified start_date is set a date. So send notification when now is 2019-07-1
.How I can checking every day with schedule
in Laravel ?
I tried case:
Invoice::whereNotNull('start_date')->whereDate('start_date','....');
Actually, the scenarios I wanted to do were these. For examples,
It should be first send notification date is 2019-07-01
(30 days before every year)
It should be second send notification date is 2019-07-15
(15 days before every year)
It should be third send notification date is 2019-07-26
(5 days before every year)
And always should be repeat these steps as next year
I found the solution like this. Firstly, I did modified My Table like this.
| service_name | start_date(date) | next_notification_date(date) | next_add_day(int) |
|:-------------|-----------------:|:----------------------------:|:-----------------:|
| example | 2018-08-01 | NULL | NULL |
And I create a command class to the process.
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,
]);
});
}
And finally I add command in the Console\\Kernel.php for control twice in day
$schedule->command('expiration-control')->twiceDaily();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.