Laravel / Laravel Artisan and CLI

Creating and Using Custom Commands

This tutorial will guide you through the process of creating and using custom Artisan commands in Laravel. You will learn how to define your own commands and use them in your Lara…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Explores Laravel's Artisan command-line interface for automation and productivity.

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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help