Posts Learn Components Snippets Categories Tags About
/

Laravel Custom Command Console Color

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

Created on Sep 11, 2021

10 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</>");

Snippets by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

Load comments for Laravel Custom Command Console Color

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your project.

View Components

Other Code Snippets

More