Created
December 23, 2019 17:09
-
-
Save jorgecrodrigues/57044182728c41b510dbeb9f2e49ba4d to your computer and use it in GitHub Desktop.
Progress bar for seed, Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks