Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Progress Bar while Seeding a database in Laravel

Tags:

I have to seed quite a lot of data into a database and I want to be able to show the user a progress bar while this happens. I know this is documented:

  • https://laravel.com/docs/master/artisan#registering-commands (just above)
  • http://symfony.com/doc/2.7/components/console/helpers/progressbar.html

but I'm having problems including it in my seeder.

<?php  use Illuminate\Database\Seeder;  class SubDivisionRangeSeeder extends Seeder {     public function run()     {         $this->output->createProgressBar(10);         for ($i = 0; $i < 10; $i++) {             sleep(1);             $this->output->advance();         }         $this->output->finish();     } } 

or

<?php  use Illuminate\Database\Seeder;  class SubDivisionRangeSeeder extends Seeder {     public function run()     {         $this->output->progressStart(10);         for ($i = 0; $i < 10; $i++) {             sleep(1);             $this->output->progressAdvance();         }         $this->output->progressFinish();     } } 

from https://mattstauffer.co/blog/advanced-input-output-with-artisan-commands-tables-and-progress-bars-in-laravel-5.1

Any ideas?

like image 939
Robert Johnstone Avatar asked May 19 '16 09:05

Robert Johnstone


People also ask

What does Laravel use for seeding?

Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.


1 Answers

You can get access to output through $this->command->getOutput()

public function run() {     $this->command->getOutput()->progressStart(10);     for ($i = 0; $i < 10; $i++) {         sleep(1);         $this->command->getOutput()->progressAdvance();     }     $this->command->getOutput()->progressFinish(); } 
like image 74
Dmitry Avatar answered Sep 23 '22 11:09

Dmitry