Question

How do I create a simple Artisan command in Laravel?

Hi all,

I’m quite new to Laravel and so far I really like it, especially the artisan commands. Has anyone created a custom artisan command? If so can you please share the steps that I would have to take in order to do so?

Thanks!


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Hello,

Yes, artisan is great, you could use it to automate a lot of tasks.

I’ll try to walk you through the process of creating a simple “Hello World!” artisan command.

Prerequisites:

  • You need to have Laravel installed. You can follow the steps on how to do that here if you don’t have it installed yet:

https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-laravel-with-lemp-on-ubuntu-18-04

  • Step 1

First, we will build the command. To create a new Artisan command you can use the make:command artisan command:

php artisan make:command HelloWorld

The output that you would get should look something like this:

Output:

  1. Console command created successfully.

This would create a file called HelloWorld.php in the app/Console/Commands directory.

The content of the file should be similar to this one here:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class HelloWorld extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}
  • Step 2

Once you have your command generated, you need to update the name and the description properties for the new HelloWorld class. To keep things as simple as possible I would just use hello as the name and Prints Hello World as the description.

...
    protected $signature = 'hello:world';
...
    protected $description = 'Prints Hello World';
...

Now you should be able to see your new command in the Artisan list:

php artisan list | grep -i hello

Output:

  1. hello:world Prints Hello World
  • Step 3

Now if you run your command: php artisan hello:world you would not get any output. This is because you do not yet have any content in your handle method. That is where you need to put the logic for your command and the things that you would like it to do. In our case we want to just return Hello World! as the output, so the piece of code that needs to go there is:

...
    public function handle()
    {
        $greeting = "Hello World!";
        print "$greeting \n";
    }
...

That is pretty much it, now if you run your command you should see the following output:

  1. php artisan hello:world

Output:

  1. Hello World!

This is pretty much it, of course, this is a really basic example, you could add a lot of logic and arguments depending on what you would like your command to do, but this should be enough to get you started!

Also, I would recommend checking the official documentation here:

https://laravel.com/docs/5.0/commands

Hope that this helps!

Regards, Bobby

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

When I type ls, I can see my files, but when I type php artisan… I got the message: Could not open input file: artisan

I’m in /srv/users/myappname/apps/myappname/public

all the files from my laravel project are in public folder.

Thanks for guidance as I’m not a developper.

When I type: php artisan make:command HelloWorld in the console, I get this error message: Could not open input file: artisan

@bobbyiliev Great explanation!

For the most up to-date documentation here is a link to the latest Laravel version 6: https://laravel.com/docs/6.x/artisan