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 ?
Reasons why php artisan serve not workingYou are running php artisan serve command in wrong directory. php artisan serve is laravel command only work with laravel project. Check project directory and run in root directory of your project. The requested host or port is not available.
Create a console command by running the "artisan make:command" in your terminal. The content of the console command class should be as follows. Anytime the you run "site:generate-sitemap" command, the generate sitemap jobs will be dispatched and the sitemap will be generated.
Laravel Artisan Creating and registering new artisan command after creating command you can register your command inside app/Console/Kernel. php class where you will find commands property. so you can add your command inside the $command array like : protected $commands = [ Commands\[commandName]::class ];
Try to check output by calling
dd(Artisan::output());
after
$export = Artisan::call('languages:export');
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With