Ad

Command Not Being Found Even Though I Am Registering It Laravel 6.x

- 1 answer

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 App\Modules\Core\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Foundation\AliasLoader;
use App\Modules\Core\Handlers\RedirectHandler;
use App\Modules\Core\Console\Commands\CreateAdminUser;
use App\Modules\Core\Values\IsMedicalRecordEmpty;

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 app\Console\Kernel, 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
     */
    App\Modules\Core\Providers\CoreProvider::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 App\Modules\Core\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use Illuminate\Auth\Events\Verified;
use App\Modules\Core\Users\Mail\GeneratedAdmin;
use App\Modules\Core\Users\Models\User;
use App\Modules\Core\Users\Services\RegisterUserService;

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?

Ad

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...

 Whoops\Exception\ErrorException  : Cannot redeclare App\Modules\Core\Providers\CoreProvider::register()

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

Ad
source: stackoverflow.com
Ad