Hiding Laravel Artisan Console Commands from the list of available commands

Hiding Laravel Artisan Console Commands from the list of available commands

When you are releasing products or packages to the world sometimes you may have some Artisan Console commands that are only used for installation, or other special circumstances that you don’t want to expose in the list when someone runs php artisan.

Hiding Using the Command hidden property

Laravel makes has a simple way to hide a command by setting a hidden property on the command class like this:

class DestructiveCommand extends Command
{
    protected $signature = 'db:resetdb';
    protected $description = 'DESTRUCTIVE! do not run this unless you know what you are doing';

    // Hide this from the console list.
    protected $hidden = true;

Hiding Using the setHidden method

If you’d like to take this a step further Brian Dillingham shared a great tip on Twitter on how to do this programmatically using the setHidden method:

Here is a more copy/paste friendly version of the code in his screenshot:

class Install extends Command
{
    protected $signature = 'package:install';
    protected $description = 'Install Package';

    public function __construct() 
    {
        parent::__construct();
        if (file_exists(config_path('package.php'))) {
            $this->setHidden(true);
        }
    }

With either of these set your console command will no longer show when running php artisan in the console, of course, someone can still run it manually if they know the signature so this is only useful for hiding in the list, not from stopping it from actually running.

Filed in: News / Artisan

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Software Developer
Remote (US & Canada)
Alley
😎 Laravel Developer
Remote
Jogg, Inc
Junior, Mid, and Senior Software Engineers. Laravel / Vue. Saint Petersburg, FL
Saint Petersburg, FL and Remote
ShineOn
Senior PHP Developer (Laravel)
Remote
The Interaction Design Foundation
Fullstack Laravel Engineer (Munich/Remote) 💻
Munich, Germany or Remote
AirLST GmbH