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

Sev*_*ths 16 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

<?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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

有任何想法吗?

Dmi*_*try 37

您可以通过访问输出 $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();
}
Run Code Online (Sandbox Code Playgroud)

  • 那太好了你甚至不需要自己的工匠命令.只需放入它就可以了.谢谢.@Sevenearths这可能应该是你的答案,因为它完全不需要自定义命令就可以实现你的例子. (2认同)
  • 这应该是答案 (2认同)