Skip to content
Advertisement

Using a Progress Bar while Seeding a database in Laravel

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:

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

<?php

use IlluminateDatabaseSeeder;

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 IlluminateDatabaseSeeder;

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?

Advertisement

Answer

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();
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement