Menü schliessen
Created: May 27th 2025
Categories: IT Development,  Laravel,  Php
Author: Milos Jevtic

Using Progress Bars in Laravel Artisan Commands

Tags:  CLI,  Laravel
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Laravel's Artisan CLI is great for building custom commands. But when your script runs longer than a few seconds, it’s useful to give the user some visual feedback. That’s where Laravel’s progress bars come in. You can show how far along your command is.

Basic Progress Bar Example

Laravel gives you possibility to display progress of your CLI command.

class ImportUsers extends Command
{
    protected $signature = 'secrets:import-users
                            {file : A CSV with the list of users}';

    protected $description = 'Import users from a CSV file.';

        public function handle()
    {
        $lines = $this->parseCsvFile($this->argument('file'));

        $progressBar = $this->output->createProgressBar($lines->count());
        $progressBar->start();

        $lines->each(function ($line) use ($progressBar) {
            $this->convertLineToUser($line);
            $progressBar->advance();
        });

        $progressBar->finish();
    }

    // class specific methods
}

What’s Happening Here?

  • createProgressBar(): Creates a progress bar with total steps.
  • start(): Initializes and displays the progress bar in the terminal.
  • advance(): Moves the bar forward after each step.
  • finish(): Completes the bar and finalizes the output.

Use Cases

  • Data Import: Show progress as you process rows from a file.
  • API Sync: Give visual feedback during external API pulls.
  • Batch Processing: Show status while converting or updating records.

Common Pitfalls

  • Progress bar doesn’t show: Only works in CLI context, not web or queue jobs.
  • Output looks messy: Add $this->newLine() after finish() for clean formatting.

Conclusion

Adding a progress bar to your Laravel command is a small touch that can make a big difference. It gives users better visibility into what’s happening and makes your CLI scripts feel more polished. It’s easy to implement and helps improve the experience for anyone running your command.