When outputting information on the command line, it quickly gets messy. An easy solution may be to add new lines to break up the output, but that doesn't provide context to the user. Adding some colors to the mix makes messages clear and adds emphasis to the output.

Laravel makes it easy to add colors to your output by using one of the default methods like line, warn, error, comment, question, and info. In this case, the line method will be the default color. The warn method will show yellow text. The error will display white text on a red background. The question method displays black text on a cyan background. Lastly, the info method will display text in green.

// Default color
$this->line('This is a line');

// Yellow text
$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 text
$this->info('This is some info');

Console colors are defined by the terminal itself. Green for example could be colored differently to what you expect, based on the settings in your terminal.

Laravel already offers some cool out-of-the-box options, but we can even take it a step further. When writing to the command line, we can define the foreground color, and the output's background color. Let's see how we can do that:

$this->line('<bg=black> My awesome message </>');
$this->line('<fg=green> My awesome message </>');
$this->line('<bg=red;fg=yellow> My awesome message </>');

The abbreviation bg stands for background, while fg stands for foreground (the text color).

Keep in mind that you might need to add extra spaces around the text to make it more readable because of the bold background color.

Apart from that, we can add more options like bold text to emphasize the output.

$this->line('<options=bold;fg=red> MY AWESOME MESSAGE </>');
$this->line('<options=underscore;bg=cyan;fg=blue> MY MESSAGE </>');

We may pick one of the following colors: black, red, green, yellow, blue, magenta, cyan, white, default, and one of the following options for the font style: bold, underscore, blink, reverse, conceal. The reverse option can be handy to swap the background and foreground colors.