Home > database >  Schedule a function to run each day in the backend [LARAVEL]
Schedule a function to run each day in the backend [LARAVEL]

Time:10-25

Each day at 00:00 I would like to execute a function that performs an update operation on certain records in the mysql database (It will check if any machines have passed the allowed detection interval). I would like to know the best way to do this in Laravel (v9.0)

Thanks in advance

CodePudding user response:

For laravel :

goto app->console->kernel.php

then, Write all the function/commands inside schedule functions.

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
        //Do Something here!!
    })->daily();
}

or if you want to run some artisan function say queue:work:

protected function schedule(Schedule $schedule)
{
    //Start queue works every minutes
    $schedule->command('queue:work --stop-when-empty')->everyMinute();

}

If you want to run scheduler at specified time use :

->dailyAt('13:00') instead of ->daily()

You will need to run this command in console:

php artisan schedule:run

For reference: https://laravel.com/docs/9.x/scheduling#defining-schedules

  • Related