如何安排时基查询?在拉拉维尔

我正在使用此查询来更新状态值


public function updateStatus(Request $request)

{

    $customer = Customer::findOrFail($request->user_id);

    $customer->status = $request->status;

    $customer->new_customer_status = 1;

    $customer->save();

    return response()->json(['message' => 'User status updated successfully.']);

}

我希望如果status == 1一周后$customer->new_customer_status应该自动变成NULL


如何安排基于时间或天数的查询在一周后或给定时间自动运行?


小怪兽爱吃肉
浏览 113回答 2
2回答

慕少森

发出命令php artisan make:command UpdateUserNotNew在该类 app/Console/Commands/UpdateUserNotNew.php 中设置命令名称:protected $signature = 'cron:update-user-not-new';根据处理方法:public function handle(){&nbsp; &nbsp; Customer::whereDate('created_at', '>', now()->addDays(7)->toDateTimeString())&nbsp; &nbsp; &nbsp; &nbsp; ->update([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'new_customer_status' => null&nbsp; &nbsp; &nbsp; &nbsp; ]);}在 app/Console/Kernel.php 中的schedule()方法下添加以下行:protected function schedule(Schedule $schedule){&nbsp; &nbsp;$schedule->command('cron:update-user-not-new')->daily();&nbsp; //<--- this line}为了使所有这些工作正常,您必须在服务器上启用 crontab,该信息位于 Laravel 文档中:https://laravel.com/docs/7.x/scheduling#introduction

慕桂英546537

您必须将查询放入排队作业中。然后,您可以安排该作业在一周后运行。这是文档中给出的示例,就在这里:https ://laravel.com/docs/7.x/queues#delayed-dispatchingProcessPodcast::dispatch($podcast)->delay(now()->addMinutes(10));ProcessPodcast工作等级在哪里。我将让您进一步深入文档以了解如何创建作业,但这确实非常简单明了。当然,就您的情况而言,您可能应该这样做now()->addWeek()而不是now()->addMinutes(10).
打开App,查看更多内容
随时随地看视频慕课网APP