DEV Community

Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

How to Create Schedule Command in Laravel 11

In this article, we'll see how to create a schedule command in laravel 11.

In Laravel 11, the process of setting up cron job task scheduling commands has changed.

The Kernel.php file, traditionally used for this purpose, has been removed.

Instead, cron jobs are now defined directly in the console.php file. Below, you'll find a straightforward example illustrating how to create a new command and configure it within Laravel 11

So, let's see the laravel 11 scheduler command, laravel scheduler.

Create a new command using the following command.

php artisan make:command TestJob
Enter fullscreen mode Exit fullscreen mode

Now, you will find the newly created TestJob.php file within the Console directory.

app/Console/Commands/TestJob.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class TestJob extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:test-job';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        info('Command runs every minute.');
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, register a command into the console.php file.

routes/console.php

<?php

use Illuminate\Support\Facades\Schedule;

Schedule::command('app:test-job')->everyMinute();
Enter fullscreen mode Exit fullscreen mode

You can configure a cron job in the crontab file, and the output will be displayed as follows:

[2024-03-16 17:32:30] local.INFO: Command runs every minute.  
[2024-03-16 17:33:30] local.INFO: Command runs every minute.  
[2024-03-16 17:34:30] local.INFO: Command runs every minute. 
Enter fullscreen mode Exit fullscreen mode

You might also like:

Read Also: Laravel 11: Health Check Routing Example

Top comments (0)