Posts Learn Components Snippets Categories Tags Tools About
/

Laravel Custom Command Console Color

Learn how to define custom artisan command with different console color.

Created on Sep 11, 2021

1931 views

To create a custom artisan command in Laravel, you can make use of the "php artisan make:command [your-command-name]" command. This command accepts the name that describes the command. Imagine creating a command called "Inspire" below is how you generate it.
php artisan make:command Inspire
The class that will be generated will be as follows.
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class Inspire 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 int
     */
    public function handle()
    {
        return 0;
    }
}

Custom Console Color


It's very easy to define a custom console command with different colors and below are the available colors that you can print out to the terminal when running the custom console command.
// Default color
$this->line('This is a line');

// Yellow collor
$this->warn('This is a warning');
$this->comment('This is a comment');

// White text on red background
$this->error('This is an error');

// Black text on cyan background
$this->question('This is a question');

// Green color
$this->info('This is some info');
You can call the method above within the handle function and it's defined like below.
<?php
/* other codes ommited */
public function handle()
{
    $this->info('You are running the Inspire command');

    $this->comment('Performing some processing here');

    $this->info('The command has been successfully executed');

    return 0;
}

More Colors Options


You can define more colors using the abbreviation of "bg" which stands for background and "fg" which stands for the foreground. The available colors that you can pick from any of the following colors: black, red, green, yellow, blue, magenta, cyan, white, default.
$this->line('<bg=black>Your message here</>');
$this->line('<fg=green>Your message here</>');
$this->line('<bg=red;fg=yellow>Your message here</>');
$this->line('<bg=red;fg=yellow>Your message here</>');

More Font Style Options


For the font style, you can use the following options: bold, underscore, blink, reverse, and conceal for the font style.
$this->line("<options=bold;fg=red>Your message here</>");
$this->line("<options=bold;fg=red>Your message here</>");
$this->line("<options=underscore;bg=cyan;fg=blue>Your message here</>");

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

Load comments for Laravel Custom Command Console Color

)