How to Register Schedule Command in Laravel 11?

By Hardik Savani September 4, 2024 Category : Laravel

Laravel 11 Schedule Command

Hi, In this post, I will show you how to set up schedule commands in the laravel 11 framework.

The release of Laravel 11 is around the corner, and it is packed with a lot of new features and improvements. Laravel 11 comes with a slimmer application skeleton, introducing a streamlined application structure, per-second rate limiting, health routing, etc.

In Laravel 11, the Kernel.php file for setting up cron job task scheduling command has been removed. We now need to define the cron job in the console.php file. You can see a simple example to create a new command and set it up in Laravel 11.

First, we will create a simple new command using the following command:

php artisan make:command TestJob

Now, you will see the newly created `TestJob.php` file in the Console directory. We will just add some information text to it right now. Let's update the code:

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 run every minute.');
    }
}

Now, you can simply register the created command like the example below:

routes/console.php

<?php
   
use Illuminate\Support\Facades\Schedule;
   
Schedule::command('app:test-job')->everyMinute();

You can set up a cron job in the crontab, and you'll see the output like the following:

[2024-03-08 14:34:53] local.INFO: Command run every minute.  
[2024-03-08 14:35:53] local.INFO: Command run every minute.  
[2024-03-08 14:36:53] local.INFO: Command run every minute. 

I hope it can help you...

Shares