Widgets How to save/redirect output from Laravel Artisan command


I dream in code

About the author

Robert Williams is an internet application developer for the Salem Web Network.
E-mail me Send mail
Go Daddy Deal of the Week: 30% off your order at! Offer expires 11/6/12

Recent comments




Code Project Associate Logo


The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

How to save/redirect output from Laravel Artisan command

Sometimes you need to call one Artisan Command from another, and the framework has a "call" method to handle that eventuality. Sometimes, you even need to return data from one command and process it with another, and that has always been a little tricky. The command you are calling to must write the data to the console using:


In Laravel 4 you could do this to call that command and retrieve the data:

use Symfony\Component\Console\Output\BufferedOutput;

$output = new BufferedOutput;    
$this->call('command:name', array('argument' => 'foo', '--option' => 'bar'), $output);
$result = $output->fetch();

And that worked great. However, I recently upgraded one of my PHP sites from Laravel 4 to Laravel 5 and sadly this no longer works. The reason is that the optional output argument is no longer an option. However, after looking at the code in the Command class I was able to find a way to make this work:

use Symfony\Component\Console\Output\BufferedOutput;

$output = new BufferedOutput;
$instance = $this->getApplication()->find('command:name');
$arguments = array('command' => 'command:name', 'argument' => 'foo', '--option' => 'bar');
$instance->run(new ArrayInput($arguments), $output);
$result = $output->fetch();

If the data being returned is more complex than a simple value, convert it to Json: Write it out using $this->line(json_encode(array($thumbnailList, $posterList))); and read it like this: $images = json_decode($output->fetch(), true);


Categories: PHP
Posted by Williarob on Tuesday, March 31, 2015 12:42 PM
Permalink | Comments (0) | Post RSSRSS comment feed
blog comments powered by Disqus