Skip to content

Command

Create a new command

php artisan make:command CommandName

This will generates a CommandName.php at app/Console/Commands/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

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

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        return 0;
    }
}

API

Warning

The description of the command need to insert spaces before and after the colon. e.g. {arg : description}'

Arguments

  • Define

    1
    protected $signature = 'test:command {arg1 : All jobs}';
    

  • Access

    1
    2
    3
    $this->argument("arg1"); // arg1
    
    $this->arguements();     // A list of arguments
    

Options

  • Define
1
protected $signature = 'test:command {--opt1: A option} {--opt2=} {--opt3=123}';
  • Access
1
2
3
4
5
$this->option('opt1'); // flag
$this->option('opt2'); // value if given
$this->option('opt3'); // default value if not specified

$this->options();      // A list of options

Advanced arguements or options

protected $signature = 'test:command {--Q|queue} ';

// Input array
protected $signature = 'test:command {input=*}';     // for Arguemtns

protected $signature = 'email:send {user} {--id=*}'; // for Options

Output

  • Write outputs with level
    $this->line()
    $this->info()
    $this->comment()
    $this->question()
    $this->error()