laravelconfiglaravel-artisan

Is there a way to disable artisan commands?


Is there a way to disable artisan commands from running at all?

For example, if I wanted to disable php artisan migrate:fresh from running, where would I go to remove/disable the command?


Solution

  • As far as I know, laravel does not have this feature by default. And this is still under laravel ideas.

    I also had this problem before and could not find a solution, I am not sure why you want to disable a command. But my case was that in the production environment I never want to run php artisan migrate:fresh. So what I end up with is to override the default command.

    For example, in the routes/console.php file:

    if ('production' === App::environment()) {
        Artisan::command('migrate:fresh', function () {
            $this->comment('You are not allowed to do this in production!');
        })->describe('Override default command in production.');
    }
    

    So, when you are in production, php artisan migrate:fresh will do nothing. You can change the condition based on your requirement, my example is just an idea of how you can override a laravel default command based on some variables in the .env file.

    You can do a lot of things here as well, I am not sure why you want to disable the command, so this is the best I can help.

    UPDATE JUNE 2024

    Laravel v11.9.0 introduced a new Prohibitable trait and added in a few destructive commands, migrate: fresh is one of them.

    All you need to do is add this to the boot method of your AppServiceProvider to prevent certain commands.

    public function boot(): void
    {
        FreshCommand::preventFromRunning();
        RefreshCommand::preventFromRunning();
        ResetCommand::preventFromRunning();
        WipeCommand::preventFromRunning();
    }
    

    Note: you may limit this to certain environments by passing a boolean argument to this method. For example, only preventing them in production.

    FreshCommand::preventFromRunning($this->app->isProduction());
    

    Or call all of them in one go

    DB::prohibitDestructiveCommands($this->app->isProduction());
    

    Here is the original PR