Skip to content

Instantly share code, notes, and snippets.

@brifiction
Created February 3, 2020 01:55
Show Gist options
  • Save brifiction/38eeac1be6107844fa9c98627a2ebcbb to your computer and use it in GitHub Desktop.
Save brifiction/38eeac1be6107844fa9c98627a2ebcbb to your computer and use it in GitHub Desktop.
Laravel Console - Progress Bar Example
<?php
use App\Account;
use App\User;
use Illuminate\Database\Seeder;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;
class LocalUserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// progress bar start, no default max provided
$this->command->getOutput()->progressStart();
factory(User::class, 50)->create()->each(function ($u) {
// define Account relationship with User
$account = factory(Account::class)->create([
'user_id' => $u->id,
'name' => $u->name,
'email' => $u->email
]);
// define (attach) User relationship with existing Role
$u->role()->attach(2);
// progress bar advance per user created
$this->command->getOutput()->progressAdvance();
});
// End progress bar
$this->command->getOutput()->progressFinish();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment