Skip to content
Advertisement

return array from command to controller. Laravel [closed]

I have an array in my command and I want to return the array to a controller where I call the command. This is what I want. Command :

return $array;

Controller :

$array = Artisan::call('command');

I have also tried this:

Artisan::call('command');
$array = Artisan::output();

But they both don’t work. Please help?

Advertisement

Answer

You can’t get the result of your command back in your controller, (you will only get the “text” from it)

Instead if you want directly a php array, try to export your command’s code in a helper function (or in the model you are using in your command), then call it from your controller and your command.

If you can’t do that, try this in your handle() function:

$this->line($array->toJson());

then in your controller :

Artisan::call('command');
$json = Artisan::output();
$array = json_decode($json);

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement