How to save/redirect output from Laravel Artisan command?

后端 未结 3 1936
孤独总比滥情好
孤独总比滥情好 2020-12-05 19:53

I\'m using Artisan::call() in one of my routes and would like to save the command output to a variable.

Is there any way to capture the STDOUT and STDER

相关标签:
3条回答
  • 2020-12-05 20:17

    When running a command from inside another command, here is how to get all the styling:

    public function handle()
    {
        Artisan::call('other:command', [], $this->getOutput());
    }
    
    0 讨论(0)
  • 2020-12-05 20:31

    This is a way:

    use Symfony\Component\Console\Output\BufferedOutput;
    
    Route::get('/test', function()
    {
        $output = new BufferedOutput;
    
        Artisan::call('list', array(), $output);
    
        return $output->fetch();
    });
    
    0 讨论(0)
  • 2020-12-05 20:32

    Seems the previous answers don't work in Laravel 5.2 any more (not sure about 5.1) You can now use Artisan::output();

        $output = '';       
        if (!Schema::hasTable('migrations')) {
            Artisan::call('migrate:install', array());
            $output .= Artisan::output();
        }
    
        // Updates the migration, then seed the database
        Artisan::call('migrate:refresh', array('--force' => 1));
        $output .= Artisan::output();
    
        Artisan::call('db:seed', array('--force' => 1));
        $output .= Artisan::output();
    
        dd($output);
    
    0 讨论(0)
提交回复
热议问题