Hiding Laravel Artisan Console Commands from the list of available commands

Tutorials

January 6th, 2021

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:

✨Laravel Package Tip: Hide your install commands from php artisan after installed to keep things tidy. pic.twitter.com/o4PK8xXkIk

— Brian Dillingham (@im_brian_d) January 2, 2021

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:

Eric L. Barnes

Eric is the creator of Laravel News and has been covering Laravel since 2012.