Reputation: 58652
In my Laravel application, I run
php artisan languages:export
I got a csv
file to export successfully base on my languages
table.
BUT the goal is to leverage this Artisan::call from Laravel, but when I did this in my code
$export = Artisan::call('languages:export');
I kept getting 0
as my result of $export
variable - no file exported of course.
Now I try to call it via shell_exec() and exec()
$cmd = 'php '.base_path().'/artisan languages:export';
$export = shell_exec($cmd);
I see nothing generated on either one.
From the command line interface, I run
php /Applications/MAMP/htdocs/code/artisan languages:export
I saw my csv file generated.
How would one go about and call artisan commands via code ?
Upvotes: 19
Views: 7454
Reputation: 928
Artisan:call() support $outputBuffer parameter, do you try it?
$parameters = [];
$outputBuffer = null;
Artisan::call('languages:export', $parameters, $outputBuffer);
Upvotes: 0
Reputation: 9596
Your console command isn't going to return the CSV file that way, because that's not how console commands work. The fact that you're getting 0
is actually a good thing console land (it means it completed without throwing errors).
Your call is correct, it's your expectation that is wrong. You'll need to change your approach for however you're consuming $export
. If you want to actually access the newly-created CSV, have a look at the phpleague/csv
package and/or the built in fopen
command. If you just want to know that it completed successfully, then just take that 0
as a success.
Upvotes: 8
Reputation: 524
Try to check output by calling
dd(Artisan::output());
after
$export = Artisan::call('languages:export');
Upvotes: 9
Reputation: 161
You could also try the exec()
function with php artisan languages:export
as parameter. You may need to include the right path in front of the command in that case.
I've also found the function Artisan::command
instead of call, maybe that works?
Upvotes: 5