Creating and Using Custom Commands

Tutorial 2 of 5

Creating and Using Custom Commands in Laravel

1. Introduction

In this tutorial, we are going to learn how to create and use custom Artisan commands in Laravel. Laravel's Artisan command-line tool provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component. We will learn how to define and use our own custom commands within our Laravel projects.

By the end of this tutorial, you will be able to:
- Create custom Artisan commands
- Understand how to use these commands in your Laravel applications

Before we begin, you should have a basic understanding of Laravel and PHP.

2. Step-by-Step Guide

Artisan commands in Laravel are stored in the app/Console/Commands directory. Laravel's make:command Artisan command allows you to generate a new command class in this directory.

Creating a New Command

Run the following command to create a new Artisan command:

php artisan make:command MyCustomCommand

This will create a new command class in app/Console/Commands/MyCustomCommand.php.

Defining Input Expectations

In the created command file, two properties need to be filled in: $signature and $description. $signature is the command that will be typed into the console, and $description is a brief explanation of what the command does.

protected $signature = 'my:command';
protected $description = 'This is my custom command';

Command Logic

The handle method will contain the logic of the command. This method is called when your command is executed.

public function handle()
{
    $this->info('My custom command is working');
}

Registering The Command

After creating and defining your new command, you need to register it with Artisan. This is typically done in the commands method of your App\Console\Kernel class.

protected $commands = [
    Commands\MyCustomCommand::class,
];

Running The Command

To run your new custom command, you may use the Artisan::call method:

\Artisan::call('my:command');

Or simply in your terminal:

php artisan my:command

3. Code Examples

Here's how your MyCustomCommand.php should look:

namespace App\Console\Commands;

use Illuminate\Console\Command;

class MyCustomCommand extends Command
{
    protected $signature = 'my:command';

    protected $description = 'This is my custom command';

    public function handle()
    {
        $this->info('My custom command is working');
    }
}

And here's the Kernel.php:

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    protected $commands = [
        Commands\MyCustomCommand::class,
    ];

    protected function schedule(Schedule $schedule)
    {
        // Your task schedule
    }
}

When you run php artisan my:command, you should see the text "My custom command is working" in your terminal.

4. Summary

In this tutorial, we learned how to create a custom Artisan command in Laravel and use it. This is a powerful feature that will allow you to automate various aspects of your application development and deployment process.

For further learning, you can explore how to schedule artisan commands and how to use different types of input and output methods available.

5. Practice Exercises

  1. Create a command that lists all the users in your database.
  2. Create a command that creates a new user in your database.
  3. Create a command that updates a user's information in your database.

Remember, the best way to learn is by doing. So, play around with different command options and see what you can come up with. Good luck!