Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading