markuskoehler's avatar

Artisan command from package not recognized when calling from PHP

Hi all,

I have an issue when I try to call an artisan command from PHP through the Artisan::call() syntax. I want to implement this in one package and the command comes from another package, both are obviously used in the same app. When executing "php artisan" from the cmd, all looks good and the command is found, but not when calling like explained through PHP. Any idea on that?

Screenshot

EDIT: Sorry, I was not specific about the actual error. The error is something like "There were no commands found in the 'workflow' namespace." So I am definitely using Artisan Facade correctly, the issue must be deeper...

0 likes
7 replies
Dunsti's avatar

You need to either use Artisan in your class, or you need to access it like this:

$exitCode = \Artisan::call(...
markuskoehler's avatar
markuskoehler
OP
Best Answer
Level 1

This question is related and I found a workaround by using exec()... Not a very nice way and I still would like to understand why Artisan::call() does not work with commands from packages, but at least it is working...

EDIT: Found the issue together with our Laravel consultant. In the package registering the console commands, I was using the if($this->app->runningInConsole()) check, of course it is only registered then if the app runs in console. By removing this constraint, the issue was fixed and Artisan::call() was able to find my command.

2 likes
Jmac's avatar

Wow, did the same mistake ( $this->app->runningInConsole()) in my package service provider! Thanks for reporting back, probably saved me a hour or two.

mjlovefl's avatar

You can also simply add the command yourself in the Console Kernel and then it will be found.

In the file: app/Console/Kernel.php

Add it to:

protected $commands = [
    \Package\Path\Console\CommandName::class,
];
3 likes
patake's avatar

Hi all,

I did everything that was posted here and still having the same issue. Running the command on console works but inside another command not. Looking on laravel.log, nothing there.

On my tests, the 'right' way (as per documentation) should be

$parameters = [
            'type'     => 'foobar',
            'duration' => 10,
            'user'     => 103,
            'client'   => 14,
        ];
Artisan::call('sync:my-custom-command', $parameters);
dd(Artisan::output());

All I was receiving was null.

But, desperately, trying this line

Artisan::call('sync:my-custom-command foobar 10 103 14');
dd(Artisan::output());

The command works as expected.

I'm using

  Laravel Version . 10.8.0  
  PHP Version . 8.2.3  

There is something wrong on the Artisan::call command or I'm missing something?

Please or to participate in this conversation.