Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run artisan command from php function

I want to run php artisan passport:client --password from function.

I tryed Artisan::call('passport:client'); and Artisan::command('passport:client'); but it return undefined command

Note: I have alredy install laravel passport and the command is working fine from the terminal

like image 969
Yousuf Rabee Avatar asked Jan 12 '17 14:01

Yousuf Rabee


2 Answers

I have found it, in boot() method of the PassportServiceProvider there is a check that essentially prevent it being called from Artisan::call.

//PassportServiceProvider.php at line 37: 

if ($this->app->runningInConsole()) {

$this->commands([
    Console\InstallCommand::class,
    Console\ClientCommand::class,
    Console\KeysCommand::class,
]);
...
}

to make it work with general artisan command, we can register these commands ourselves. somewhere within the boot method of AuthServiceProvider maybe.

public function boot() {
    $this->commands([
        Console\InstallCommand::class,
        Console\ClientCommand::class,
        Console\KeysCommand::class,
    ]);
}

Now we can call Artisan::call('passport:install') or the other 2 commands.

like image 147
Mohammadreza Ghorbani Avatar answered Oct 09 '22 03:10

Mohammadreza Ghorbani


From Laravel Docs

Route::get('/foo', function () {
$exitCode = Artisan::call('email:send', [
    'user' => 1, '--queue' => 'default'
]);

//
});
like image 33
Bara' ayyash Avatar answered Oct 09 '22 05:10

Bara' ayyash