【问题标题】:Using a Progress Bar while Seeding a database in Laravel在 Laravel 中播种数据库时使用进度条
【发布时间】:2016-09-16 02:59:15
【问题描述】:

我必须将大量数据播种到数据库中,并且我希望能够在发生这种情况时向用户显示进度条。我知道这是记录在案的:

但在我的播种机中包含它时遇到问题。

<?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();
    }
}

<?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();
    }
}

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

有什么想法吗?

【问题讨论】:

    标签: php symfony laravel


    【解决方案1】:

    这是我的实现代码,

    <?php
    
    use Illuminate\Database\Seeder;
    
    class MediaTypeTableSeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            $media_types = [
                ['name'=>'movie'],
                ['name'=>'channel'],
                ['name'=>'drama'],
                // ['name'=>'ebook'],
                // ['name'=>'shopping'],
                // ['name'=>'reseller'],
                // ['name'=>'broadcasting']
            ];
            $this->command->getOutput()->progressStart(count($media_types));
            App\Models\Backoffice\MediaType::query()->delete();
            foreach($media_types as $media_type){
                App\Models\Backoffice\MediaType::create($media_type);
                $this->command->getOutput()->progressAdvance();
            }
            $this->command->getOutput()->progressFinish();
        }
    }
    

    【讨论】:

      【解决方案2】:

      以防万一其他人遇到类似情况并正在寻找解决方案,我创建了以下基类:

      <?php
      
      use Illuminate\Database\Schema\Blueprint;
      
      class TestCase extends Illuminate\Foundation\Testing\TestCase
      {
          protected $progress = 0;
      
          protected function outputInfo($text)
          {
              $this->output('info', $text, 34);
          }
      
          protected function outputSuccess($text)
          {
              $this->output('success', $text, 32);
          }
      
          protected function outputWarning($text)
          {
              $this->output('warning', $text, 33);
          }
      
          protected function outputError($text)
          {
              $this->output(strtoupper('error'), $text, 31);
          }
      
          private function output($status, $text, $colour_code = 34)
          {
              $this->newLine();
              print "\033[1;" . $colour_code . "m" . str_pad($status.':', 8) . " " . $text . " \033[0m";
          }
      
          protected function progressDots()
          {
              if ($this->progress % 20 == 0) {
                  print '.';
              }
          }
      
          protected function newLine()
          {
              echo "\n";
          }
      
          /**
           * Prevents a warning on the command line
           */
          public function testDefault()
          {
              $this->assertTrue(true);
          }
      }
      

      然后像这样使用它......

      <?php
      
      class SomethingTests extends TestCase
      {
      
          public function testSomething()
          {
              $array = range(0, 100);
      
              foreach ($array as $value) {
      
                  $this->outputInfo('Testing '.$value);
      
                  $this->assertGreaterThan(101,$value,'101 is not greater then '.$value);
      
                  $this->progressDots();
      
              }
      
              $this->newLine();
      
          }
      
      }
      

      【讨论】:

        【解决方案3】:

        您可以通过$this-&gt;command-&gt;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();
        }
        

        【讨论】:

        • 太好了,您甚至不需要自己的工匠命令。只需将其放入其中即可。谢谢。 @Sevenearths 这可能应该是您的答案,因为它完全符合您在示例中的操作,甚至不需要自定义命令。
        • 这应该是答案
        【解决方案4】:

        您的代码不起作用,因为您在错误的类中使用了$this-&gt;output。如果您再次查看您分享的文章,$this-output 用于 Artisan 命令类。

        事实上,每个 Artisan 命令都是使用 Symfony Console 组件生成的。

        您实际上是在尝试在数据库播种器中使用它:)

        我的建议:构建您的播种机,然后根据您的需要使用“安装”或“播种”自定义命令调用它们。

        【讨论】:

        • 是的,但是自定义命令将无法知道播种机在播种数据循环中的位置以增加进度条
        • 当然你需要创建一些更复杂的东西,普通的播种机是不够的:)
        猜你喜欢
        • 1970-01-01
        • 2014-03-01
        • 2015-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-10
        • 2021-10-29
        相关资源
        最近更新 更多