Monday, January 3, 2022

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

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;
    }
}

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

php artisan progressbar:test

No comments:

Post a Comment