当我单击“保存”按钮时,它将收集任务详细信息(名称,消息,经过时间),然后从其发布的当前日期开始计算失效日期。
public function store(Request $request)
{
$currentDate = date("m-d-Y H:i:s");
$currentDate_timestamp = strtotime($currentDate);
$endDate_months = strtotime($request->lapse_period, $currentDate_timestamp);
$packageEndDate = date("m-d-Y H:i:s", $endDate_months);
echo " <br> " . $packageEndDate . " <br> ";
Task::create([
'name' => $request->name,
'message' => $request->message,
'lapse_period' => $request->lapse_period,
'expires_at' => $packageEndDate
]);
return redirect()->back();
}
那行得通,问题是:如何创建一个每分钟运行一次的任务,并删除所有失效日期小于当前日期的记录?
这是我在内核中尝试过的方法,但是一分钟后什么也没发生:
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$currentDate = date("m-d-Y H:i:s");
$tasks = Task::all();
foreach ($tasks as $task) {
if($date->expires_at < $currentDate) {
$task->delete();
}
}
})->everyMinute();
}
答案 0 :(得分:2)
尝试以下代码:
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
Task::where('expires_at','<', Carbon::now())->delete();
})->everyMinute();
}
答案 1 :(得分:0)
尝试
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$currentDate = \Carbon\Carbon::now();
$tasks = Task::all();
foreach ($tasks as $task) {
$dateTask = new \Carbon\Carbon($task->expires_at);
if($dateTask < $currentDate) {
$task->delete();
}
}
})->everyMinute();
}