Skip to content

Instantly share code, notes, and snippets.

@jorgecrodrigues
Created December 23, 2019 17:09
Show Gist options
  • Save jorgecrodrigues/57044182728c41b510dbeb9f2e49ba4d to your computer and use it in GitHub Desktop.
Save jorgecrodrigues/57044182728c41b510dbeb9f2e49ba4d to your computer and use it in GitHub Desktop.
Progress bar for seed, Laravel
<?php
use Illuminate\Database\Seeder;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;
class UsersTableSeeder extends Seeder
{
/**
* Amount.
*
* @var int
*/
private $amount = 100000;
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// The output
$output = new ConsoleOutput();
// creates a new progress bar (50 units)
$progressBar = new ProgressBar($output, $this->amount);
// starts and displays the progress bar
$progressBar->start();
factory(\App\User::class, $this->amount)->make()->each(function ($user) use ($progressBar) {
// advances the progress bar 1 unit
if ($user->save()) {
$progressBar->advance();
}
// you can also advance the progress bar by more than 1 unit
// $progressBar->advance(3);
});
// ensures that the progress bar is at 100%
$progressBar->finish();
$output->write(' Finished', true);
}
}
@OmarHossamEldin
Copy link

cool tool

@yunus-floo
Copy link

thanks

@umaraziz0
Copy link

This is neat, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment