abkrim
abkrim

Reputation: 3692

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  

Upvotes: 4

Views: 3495

Answers (1)

Ugo T.
Ugo T.

Reputation: 1078

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();

Upvotes: 9

Related Questions