php – 在Laravel中播种数据库时使用进度条

前端之家收集整理的这篇文章主要介绍了php – 在Laravel中播种数据库时使用进度条前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我必须将大量数据植入数据库中,并希望能够在发生这种情况时向用户显示进度条.我知道这是记录在案的:

> https://laravel.com/docs/master/artisan#registering-commands(刚刚上方)
> http://symfony.com/doc/2.7/components/console/helpers/progressbar.html

但我在播种机中遇到问题.

  1. <?PHP
  2.  
  3. use Illuminate\Database\Seeder;
  4.  
  5. class SubDivisionRangeSeeder extends Seeder
  6. {
  7. public function run()
  8. {
  9. $this->output->createProgressBar(10);
  10. for ($i = 0; $i < 10; $i++) {
  11. sleep(1);
  12. $this->output->advance();
  13. }
  14. $this->output->finish();
  15. }
  16. }

要么

  1. <?PHP
  2.  
  3. use Illuminate\Database\Seeder;
  4.  
  5. class SubDivisionRangeSeeder extends Seeder
  6. {
  7. public function run()
  8. {
  9. $this->output->progressStart(10);
  10. for ($i = 0; $i < 10; $i++) {
  11. sleep(1);
  12. $this->output->progressAdvance();
  13. }
  14. $this->output->progressFinish();
  15. }
  16. }

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

有任何想法吗?

您可以通过$this-> command-> getOutput()访问输出
  1. public function run()
  2. {
  3. $this->command->getOutput()->progressStart(10);
  4. for ($i = 0; $i < 10; $i++) {
  5. sleep(1);
  6. $this->command->getOutput()->progressAdvance();
  7. }
  8. $this->command->getOutput()->progressFinish();
  9. }

猜你在找的Laravel相关文章