Understanding how commands are loaded in Laravel 11 and how to schedule them

Laravel has gone a long way in ensuring that creating web applications is pure bliss, not a pain. This it does by providing features that makes dev lives easier, and one of such features is its feature-rich commands and task scheduling system, which includes a powerful console kernel that simplifies command-line interfaces.

Ever wondered how Laravel automatically discovers and loads your console commands? Or maybe how you can schedule these commands to run at specific intervals? Let's look at how command loading works in Laravel 11 and explore how to easily schedule these commands.

Command Auto-Discovery

Laravel leverages the service container for auto-discovering commands located within the app/Console/Commands directory. This feature makes adding new commands as seamless as creating a new PHP file.

Let's walk through this process with an example:

// app/Console/Commands/SendEmails.php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Order;

class SendEmails extends Command
{
    protected $signature = 'emails:send';

    public function handle()
    {
        Order::where('requires_email', true)->each(function ($order) {
            // email sending logic.
            echo "Sending email to {$order->user->email}.\n";
        });
    }
}

In this example, SendEmails.php is a command that sends emails for orders. Laravel's auto-discovery feature automatically registers this command, and can run it using php artisan emails:send.

Manual Command Registration

While auto-discovery simplifies everything, there might be situations where you want to register commands manually. To do this, use the withCommands method within the bootstrap/app.php file:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use App\Domain\Orders\Commands\SendEmails;

return Application::configure(basePath: dirname(__DIR__))
		...
    ->withCommands([
			SendEmails::class,
		])->create();
Databases in VS Code? Get DevDb

In this snippet, we've explicitly registered SendEmails using the withCommands method. This can be particularly useful if your commands are located in other directories or namespaces.

Scheduling Commands

Laravel awesome scheduling system allows you to define how often specific Artisan commands should execute. This is particularly handy for tasks like sending periodic emails, running reports, etc.

Here's an example of scheduling the emails:send command from our previous example to run every 5 minutes:

// routes/console.php
Schedule::command('emails:send')->everyFiveMinutes();

Conclusion

Understanding how commands are loaded and scheduled in Laravel 11 can greatly enhance your development workflow. By taking advantage of Laravel's auto-discovery feature, you can focus on writing high-quality logic rather than managing command registration manually. Furthermore, leveraging the scheduler enables you to automate repetitive tasks efficiently.

Wanna chat about what you just read, or anything at all? Click here to tweet at me on 𝕏