Created
February 3, 2020 01:55
-
-
Save brifiction/38eeac1be6107844fa9c98627a2ebcbb to your computer and use it in GitHub Desktop.
Laravel Console - Progress Bar Example
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 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