如何在laravel中存储一天结束时的记录



例如,如果用户在一天结束前忘记批准,则一条消息将在当天结束时存储到数据库中。我使用了如下的碳格式。

if (Carbon::now()->endOfDay()->eq(Carbon::parse($phaseValue->end_date)->endOfDay())) {
// add records here
}

您必须设置一个任务调度程序,该程序将在一天结束时检查是否满足特定条件。如果没有,那么它将根据您的业务逻辑在DB或任何东西中进行插入。Laravel提供任务调度,看看:

https://laravel.com/docs/8.x/scheduling

您可以创建一个每天在给定时间运行的cron作业。为此,创建一个新命令:

php artisan make:command YourCommand

然后,你的命令看起来像这样:

<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
class YourCommand extends Command
{
//Name that will be used to call command
protected $signature = 'your:command';
protected $description = 'Command description';
public function __construct()
{
parent::__construct();
}
public function handle()
{
//Here you will handle all the logic. For ex:
$record = App/Example::first();

//Implement condition that will determine if the record will be updated
$record->update();
}
}

之后,在App/Console/Kernel.php内部创建该命令的时间表:

protected function schedule(Schedule $schedule)
{
//Name that you gave it in the protected $signature = 'your:command';
$schedule->command('your:command')->dailyAt('23:30');
}

这样,该命令将在每天23:30运行,您可以根据需要随时更新记录。你可以阅读更多关于任务调度&官方文件上的命令。

最新更新