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

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

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

routes/console.php

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

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. 

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS