If you have used the latest version of Laravel ^9.23, you should have noticed that Laravel had improved their command output styling greatly.

Yesterday I was going through a command written by one of my colleague to help us seed permissions in an easier way. I was going to add it to our office Laravel skeleton (Laravel with extra functionality to make our life easier). Just I was about to add it, I thought why not use same styling used by Laravel to style the command output. So I dug into Laravel’s code.

Like everything in Laravel, it didn't take long to figure out how they were passing outputs. Laravel uses different view components to get the desired results.

There are multiple such components and I am gonna cover most common components here.

If you want to go see the components yourself, check vendors\laravel\framework\src\ Illuminate\Console\View \Components folder in your Laravel project.

Below is the list of ways you can style your outputs.

  /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        // Line Styling
        $this->components->alert('This is an alert.');
        $this->components->info('This is a test command.');
        $this->components->error('This is an error.');
        $this->components->warn('This is a warning.');
        $this->components->line('info', 'This is a test command.'); // info, error, warn

        // Prompts
        $this->components->choice('This is a choice.', ['a', 'b', 'c']);
        $this->components->confirm('This is a confirm.');

        // Tasks
        $this->components->task('Running A Task', function () {
            sleep(1);
        });

        // Other styles
        $this->components->twoColumnDetail('First', 'Second');

        $this->components->bulletList([
            'This is a bullet list item.',
            'This is another bullet list item.',
        ]);

        return 0;
    }

The following is the corresponding outputs. Console Output

If you like these, you can go code diving to see more cool stuff like progress bar and tables.

That’s it for today, have a good day!