Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Artisan Call output in Controller?

I have a complex Artisan Command that I wanna call in my Controller also. That works. Except that it return an Exitcode instead of output.

use Symfony\Component\Console\Output\BufferedOutput; # on top

public function foobar(Request $request)
{
    $this->validate($request, [
        'date' => 'required|date_format:Y-m-d',
    ]);

    $output = new BufferedOutput;

    $exitCode = Artisan::call('foo:bar', [
        'datum' => $request->get('date'),
    ], $output);
    return $exitCode; # returns 0;
    return dd($output->fetch()); # returns ""
}

I want the output of the command. How to do that? The last line of my Artisan command has a return on the last line that should be returned.. How?

like image 436
user1469734 Avatar asked Jun 09 '16 12:06

user1469734


People also ask

How do I get out of artisan serve in php?

To execute php artisan serve stop simply goto your commnad line where your server is running and press ctrl+c. It will stop your server.

How do I exit php artisan serve in CMD?

Simply use Ctrl + C . It will come out to prompt state.

What is Artisian in Laravel?

Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component.


2 Answers

$command = 'foo:bar';

$params = [
        'datum' => $request->get('date'),
];

Artisan::call($command, $params);
dd(Artisan::output());
like image 51
zorx Avatar answered Oct 11 '22 12:10

zorx


code to output inspire phrases instead of exit code

Route::get('/wisdom', function (Request $request) {
   Artisan::call('inspire');
   return Artisan::output();
});
like image 29
Software Developer Avatar answered Oct 11 '22 11:10

Software Developer