Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

janareit's avatar

Getting Artisan::call() output?

I am trying to call Artisan command from one of my controllers like explained here: http://laravel.com/docs/5.0/artisan#calling-commands-outside-of-cli but how could I get commands output?

Inside my command I can get output for example with exec

    public function handle()
    {
        $exec_string = 'node public/js/generateBudgetDates.js';
        exec($exec_string, $output);
        dump($output);
    }

but this approach does not give me result when i run similar command from inside controller... $output is left empty then.

So again - question is how could I get data returned by generateBudgetDates.js data from command to my laravel code?

0 likes
11 replies
mass6's avatar

Have you tried returning the $output?


public function handle()
    {
        $exec_string = 'node public/js/generateBudgetDates.js';
        exec($exec_string, $output);
        return $output;
    }
pmall's avatar
pmall
Best Answer
Level 56

@janareit use proc_open to get both stderr and stdout :

    // Setup the file descriptors
    $descriptors = [
        0 => ['pipe', 'w'],
        1 => ['pipe', 'w'],
        2 => ['pipe', 'w'],
    ];

    // Start the script
    $proc = proc_open($cmd, $descriptors, $pipes);

    // Read the stdin
    $stdin = stream_get_contents($pipes[0]);
    fclose($pipes[0]);

    // Read the stdout
    $stdout = stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // Read the stderr
    $stderr = stream_get_contents($pipes[2]);
    fclose($pipes[2]);

    // Close the script and get the return code
    $return_code = proc_close($proc);
2 likes
adrian.nuernberger's avatar

Im a bit confused since you refer in your example to nodejs and not to an artisan command.

Anyways if you want to run an artisan command like php artisan help and receive the output, try this:

Route::get('/test', function()
{
    Artisan::call('help');
    dd(Artisan::output());
});
16 likes
pmall's avatar

Im a bit confused since you refer in your example to nodejs and not to an artisan command.

@painis he wants to use execute in his artisan command

Update : oh or he is confusing artisan command and commands

janareit's avatar

@pmall Thanks! Your script worked:)

@painis Yes - I specificly want to execute this node script on my server side. This is due to fact that I found one node script library performing best with generating very flexibly recurring and various types of multi-pattern calendar events (http://bunkat.github.io/later/).

Thanks guys for helping out!

Prince254's avatar

//Sample code to output inspire phrases instead of exit code
Route::get('/wisdom', function (Request $request) {
   Artisan::call('inspire');
   return Artisan::output();
});

5 likes
Prince254's avatar

Lol As long as the code works no problem

3 likes

Please or to participate in this conversation.