Skip to content
Advertisement

command not being found even though I am registering it Laravel 6.x

So consider the service provider, yes I know I am registering this command in two places, but just give me a moment to explain:

<?php

namespace AppModulesCoreProviders;

use IlluminateSupportServiceProvider;
use IlluminateContractsSupportDeferrableProvider;
use IlluminateFoundationAliasLoader;
use AppModulesCoreHandlersRedirectHandler;
use AppModulesCoreConsoleCommandsCreateAdminUser;
use AppModulesCoreValuesIsMedicalRecordEmpty;

class CoreProvider extends ServiceProvider
{

    protected $commands = [
        CreateAdminUser::class,
    ];

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(RedirectHandler::class, function($app) {
            return new RedirectHandler();
        });

        $this->app->bind(IsMedicalRecordEmpty::class, function($app) {
            return new IsMedicalRecordEmpty();
        });
    }

    public function register() {
        $this->commands($this->commands);
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        if ($this->app->runningInConsole()) {
            $this->commands([
                CreateAdminUser::class,
            ]);
        }
    }
}

So as stated before we can see that I am registering this command in two places, because I am trying to figure out why calling php artisan doesn’t show the command, it only shows if I register it in the appConsoleKernel, but because I am trying to take a modular approach to the code base, I want to register it in my service provider, to which is registered as such:

'providers' => [

    ...

    /**
     * Module Related Providers
     */
    AppModulesCoreProvidersCoreProvider::class,

    ...
],

I register the provider properly, I (yes I know I don’t need to register the command twice) register the command in the way that stack has explained it, either way should in theory work.

But alas the command does not show up when I run php artisan. At all.

The command is simple:

<?php

namespace AppModulesCoreConsoleCommands;

use IlluminateConsoleCommand;
use IlluminateSupportFacadesMail;
use IlluminateAuthEventsVerified;
use AppModulesCoreUsersMailGeneratedAdmin;
use AppModulesCoreUsersModelsUser;
use AppModulesCoreUsersServicesRegisterUserService;

class CreateAdminUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:admin {first_name} {last_name} {email}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create one admin.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        // do stuff here ...
    }
}

Any ideas?

Advertisement

Answer

Your code is redeclaring register(), first in the method with the binds, and then again with your method calling $this->command() – are you even referencing your service provider correctly? PHP should have told you this – it did for me when I tried your sample code…

 WhoopsExceptionErrorException  : Cannot redeclare AppModulesCoreProvidersCoreProvider::register()

It’s worth noting that removing the first method with the binds caused the command to show for me.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement