Description
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');
Result
I kept getting 0
as my result of $export
variable - no file exported of course.
Update
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 ?
3 Answers
Answers 1
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.
Answers 2
Try to check output by calling dd(Artisan::output());
after $export = Artisan::call('languages:export');
Answers 3
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?
0 comments:
Post a Comment