Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Artisan Commands via Code NOT Working

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 ?

like image 649
code-8 Avatar asked Jun 28 '17 20:06

code-8


People also ask

Why php artisan serve not working?

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.

How do I run artisan command in Laravel?

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.

How do I register my artisan command?

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 ];


3 Answers

Try to check output by calling dd(Artisan::output()); after $export = Artisan::call('languages:export');

like image 156
Tosin John Avatar answered Oct 03 '22 22:10

Tosin John


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.

like image 23
Shauna Avatar answered Oct 03 '22 21:10

Shauna


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?

like image 36
Jeroen Avatar answered Oct 03 '22 20:10

Jeroen