Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show progress bar on class for commands

All examples for use Progress Bar show a code easy but work on artisan command

$users = App\User::all();

$bar = $this->output->createProgressBar(count($users));

foreach ($users as $user) {
    $this->performTask($user);

    $bar->advance();
}

$bar->finish();

But I like implement on one class called from my command but does not work.

php artisan migratedb:migrate table --table=cms_users

MigrateDatabase.php

 ...
 use Abkrim\Setdart\MigrateTables;

 $migration = new MigrateTables($this->argument('type'), $this->option('table'));
 $migration->runMigration();

MigrateTables.php

... 

private function migration_cms_users() {
   ... 
   $bar = $this->output->createdProgressBar($this->getNumberRows($db));  // Error (1)
   ...
   $bar = new ProgressBar($output, $this->getNumberRows($db));  // Example on [Symfony][1] Undefined variable: output 

}


(1) [ErrorException]                                           
  Undefined property: Abkrim\Setdart\MigrateTables::$output
(2) [ErrorException]            
  Undefined variable: output  
like image 580
abkrim Avatar asked Oct 27 '25 19:10

abkrim


1 Answers

If you work on Symfony, you can define your output like this :

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;

$output = new ConsoleOutput();
$progress = new ProgressBar($output, 50);
$progress->start();

foreach($vars as $var){
    $progress->advance();
}

$progress->finish();
like image 157
Ugo T. Avatar answered Oct 29 '25 07:10

Ugo T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!