How to Save/Redirect Output from Laravel Artisan Command

How to save/redirect output from Laravel 5 Artisan command?

I managed to get this to work using Artisan::output(), which returns the output of the latest command.

Route::get('/test', function()
{
Artisan::call('testCommand', array());

return Artisan::output();
});

should do it for you.

How to save/redirect output from Laravel Artisan command?

This is a way:

use Symfony\Component\Console\Output\BufferedOutput;

Route::get('/test', function()
{
$output = new BufferedOutput;

Artisan::call('list', array(), $output);

return $output->fetch();
});

How to get Artisan::call('command:name') output in a variable?

You can call the output method on Artisan

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

Make sure you are using one of the available output methods like $this->line('output'); in the actual Artisan command. More info in the docs.

Print/Save complete Laravel Artisan Command output(large array from debug line) to file

Try using the global Log. Add a logging channel to your config/logging.php file like so:

'dataLogger' => [
'driver' => 'daily',
'path' => storage_path('logs/data.log'),
'level' => 'info',
],

And use in your code like:

use Log; 
foreach ($dataArr as $dataEle){
Log::channel('dataLogger')->info($dataEle);
}

(Or however you'd like to format your data.)

Redirect to URL from artisan command

First of all, your artisan command returns something but you're not returning anything in your route closure. So obviously, the result would be empty.

Secondly, even if you were to say return Artisan::call('...'); it won't work, because the call method returns the exit status of the console command and not the output you return in the handle method of the artisan command.

Finally, an Artisan command is never expected to return a view. Think about it, why would an artisan command return a view? Artisan commands are meant to be console commands and are not meant to return responses to requests. You have controllers for that

To fix this you can do something like this:

Route::get('customer/vat/{id}', function ($id) {
Artisan::call('app:reminder', [
'--vat' => $id
]);
return redirect()->to('/customer/123');
});

And then delete return redirect()->to('/customer/123'); from your artisan command handle method

Laravel - Write console output to log files

You can use the screen linux command.
Screen Example

and you can save the output like

#screen
#php artisan command > /etc/commandResults.log

Redirecting command line output doesn't work when running php artisan dusk

Use the --without-tty option:

php artisan dusk --without-tty tests/Browser/PeopleTest.php &> ~/Downloads/dusk.txt

Laravel artisan queue:work redirect stderr/stdout to file

Try running

nohup php artisan queue:work

it will create nohup.out file where all the output and errors will be displayed like:

[2017-03-24 00:00:00] Processed: App\Jobs\SomeJob

Getting console output in Laravel

Decided to just replace the $this->info() calls with a simple echo command and output buffer control. Looks good enough in the console and catches the data requested for emailing.

Example:

$r = processData();

if ($this->option('email-results'))
ob_start();

echo "\nSubmitted data:";
echo "\nSubmissionId: " . $r['submission_id'];
echo "\nStatus: " . $r['status'];

if ($this->option('email-results')) {
mail(
$this->option('email-results'),
'Results on ' . $start_time->toDateTimeString(),
ob_get_contents()
);

ob_end_flush();
}


Related Topics



Leave a reply



Submit