enginerd's avatar

Calling an artisan command from another artisan command

I'm trying to call an artisan command from another artisan command, which works fine, but I can't get the output of the first command into a variable.

$output = new BufferedOutput;
        
$this->call('generate:randomstring', [], $output);
$clientid = $output->fetch();

$this->call('generate:randomstring', [], $output);
$clientsecret = $output->fetch();

I'm brand new to laravel, so this approach may be quite naive. I searched for a while and I'm pretty much stuck. I've gotten plenty of output from the randomstring command via stdout, but I can't seem to capture it.

0 likes
3 replies
usman's avatar

@enginerd use the following code instead:

$this->output = new BufferedOutput;
        
$this->call('generate:randomstring');
$clientid = $this->output->fetch();

$this->call('generate:randomstring');
$clientsecret = $this->output->fetch();

But beware there will be new lines at the start and end of the output, here is the output from my console: (output of the $this->output->fetch statement)

\n
Well begun is half done. - Aristotle\n
\n

Anyways I hope you will work around it :)

Usman

3 likes
enginerd's avatar

@usman I keep forgetting about being forced to use $this in PHP. I'm so used to C# where the language is smart enough to know what you want without it... Your solution worked very well. Thanks!

Please or to participate in this conversation.