Search code examples
mysqllaraveldatetimesql-updateauto-update

Laravel auto update a field in the table when current date is more than a specific date in the table


I'm trying to update a specific field in the database table when current date is more than another specific field in the table.

My table "task":

id |..... | startDate  | endDate   | status
---|------|------------|-----------|--------
1  |..... | 2019/03/13 | 2019/03/14| Process

So, if current datetime > endDate, automatically update status to "Expired" in real time without any click.

I did create a scheduler in Kernel.php, but nothing is happening. My scheduler:

 $schedule->call(function () {
            DB::table('tasks')->whereDate('endDate', '<', date('Y/m/d H:i'))->update(['status', 'Expired']);
        })->everyMinute();

My end date value and format:

2019/03/12 17:00

Thank you in advance

P.S - I'm not using Carbon


Solution

  • I suggest to use a scheduler, and run simple command that uses DB. You can run that task every few minutes and it will do the job.

    DB::table('task')->whereDate('endDate', '<', now())->update(['status' => 'expired']);
    

    If you do not want to use Carbon, simply change now() to date('Y-m-d H:i:s')

    Remember to add single cron entry to start scheduler.

    * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1