DEV Community

Morcos Gad
Morcos Gad

Posted on

Show Progress Bar In The Terminal - Laravel

Let's start quickly. If you want to create a Progress Bar and see it in the Terminal, let us show you the steps so that it can be used in any project idea
First, we create a command

php artisan make:command ProgressBarTestCommand
Enter fullscreen mode Exit fullscreen mode

Inside app/Console/Commands/ProgressBarTestCommand.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ProgressBarTestCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'progressbar:test';

    /**
     * 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()
    {
        $items = range(1, 3);

        $progressbar = $this->output->createProgressBar(count($items));

        $progressbar->start();

        foreach ($items as $item) {
            // perform some operation
            sleep(1);

            $progressbar->advance();
        }

        $progressbar->finish();

        return 0;
    }
}
Enter fullscreen mode Exit fullscreen mode

Let's choose it and see the result and tell us about it in the comments

php artisan rogressbar:test
Enter fullscreen mode Exit fullscreen mode

I hope you enjoyed the code.
Source :- https://laravel.com/docs/8.x/artisan#progress-bars
Source :- https://www.youtube.com/watch?v=H4mxFUlR1vc

Discussion (0)