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

时间:2016-05-19 09:51:29

标签: php symfony laravel

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

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

<?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

有什么想法吗?

4 个答案:

答案 0 :(得分:27)

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

答案 1 :(得分:1)

您的代码无效,因为您在错误的班级中使用$this->output。如果您再次查看您分享的文章,则在Artisan命令类中使用$this-output

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

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

我的建议:建立你的播种机,然后用&#34;安装&#34;或者&#34;种子&#34;根据您的需求定制命令。

答案 2 :(得分: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();
    }
}

答案 3 :(得分:0)

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

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

    }

}