How to run task schedule in Laravel?

Task scheduling allows us to execute tasks, and methods automatically based on specific time periods. With Laravel schedule, we can set a schedule to run based on weekly, monthly and so on also we can customize it according to what we want. In this tutorial, we will give you an example of how to set up a schedule to delete the product from the database every minute. Note in mind that you can replace the implementation with your own or call the function in the schedule.

Set task schedule

Go to your ./app/Console/Kernel.php

<?php

namespace App\Console;

use Illuminate\Support\Facades\DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;


class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            DB::table('products')->delete();
        })->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

Run the schedule

php artisan schedule:work

The code will delete products every minute.