Search code examples
laravellaravel-artisanartisan-migrate

Laravel: Show output of a console command inside a migration?


I created a command to do some data manipulation on a very large database table and as it takes fair enough time to complete, i took the benefits of progress bar plus echoing some information on the console.

to automate stuff and reduce human errors, i want to call my command inside a laravel migration using programmatically-executing-commands style and it works but the problem is it wont print any output from corresponding command inside the console

i think i should pass the current Output-buffer that artisan:migrate is using to the Artisan::call function to make it work but had no luck to access it inside the migration

any suggestions?


Solution

  • Expanding on @ettdro's answer, the Artisan::call method has the following signature:

    Artisan::call(string $command, array $parameters = [], $outputBuffer = null);
    

    As you can see, the method accepts an output buffer as its 3rd argument. You can pass that output buffer to the method and the command logs will show up on the console.

    Here's an example:

    <?php
    
    use App\Console\Commands\YourConsoleCommand;
    use Illuminate\Database\Migrations\Migration;
    use Symfony\Component\Console\Output\ConsoleOutput;
    
    
    class SomeDbMigration extends Migration
    {
        public function up()
        {
            $output = new ConsoleOutput();
            Artisan::call(YourConsoleCommand::class, ['--some-option' => true], $output);
        }
    
        public function down()
        {
            $output = new ConsoleOutput();
            Artisan::call(YourConsoleCommand::class, ['--some-option' => false], $output);
        }
    }