Decluttering Your Laravel Console: How to Hide Artisan Commands
Technology 10 hours ago 5 min read 22 views

Decluttering Your Laravel Console: How to Hide Artisan Commands

Laravel ships with over 100 built-in Artisan commands, making it hard to find your custom commands in the noise. Learn how to create a clean, focused command list that only shows what your team actually uses—while keeping all commands fully functional.

Z

Zach Robichaud

Decluttering Your Laravel Console: How to Hide Artisan Commands

If you've ever run php artisan or php artisan list in a Laravel project, you know the output can be overwhelming. Laravel ships with over 100 built-in commands, and while they're incredibly useful, they can make it difficult to find your custom application commands in the noise.

In this post, I'll show you how to create a clean, focused Artisan command list that only displays the commands your team actually uses.

The Problem

Laravel's default php artisan list output includes commands for:

  • Database operations (db:seed, migrate:fresh, etc.)
  • Cache management (cache:clear, config:cache, etc.)
  • Code generation (make:controller, make:model, etc.)
  • Queue management (queue:work, queue:retry, etc.)
  • And dozens more...

While these commands are essential for Laravel development, most projects only use a handful of them regularly. The rest just create clutter, making it harder to discover and remember your custom application commands.

The Solution

Laravel provides a simple way to hide commands from the Artisan list using the setHidden(true) method. By creating a custom Service Provider, we can selectively hide commands we don't need while keeping our custom commands visible.

Here's a complete implementation:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Artisan;

class ConsoleServiceProvider extends ServiceProvider
{
    /**
     * Commands to keep visible (besides app/Console/Commands)
     */
    protected array $keepCommands = [
        'inspire',
    ];

    /**
     * Command namespaces/names to hide
     */
    protected array $hideNamespaces = [
        'about',
        'auth:',
        'cache:',
        'channel:',
        'clear-compiled',
        'completion',
        'config:',
        'db:',
        'db',
        'docs',
        'down',
        'env:',
        'env',
        'event:',
        'install:',
        'key:',
        'lang:',
        'make:',
        'migrate:',
        'migrate',
        'model:',
        'optimize:',
        'optimize',
        'package:',
        'pail',
        'queue:',
        'reload',
        'route:',
        'sail:',
        'schedule:',
        'schema:',
        'serve',
        'session:',
        'storage:',
        'stub:',
        'test',
        'tinker',
        'up',
        'vendor:',
        'view:',
    ];

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        // Hide unwanted commands from artisan list
        if ($this->app->runningInConsole()) {
            $this->app->booted(function () {
                $this->hideDefaultCommands();
            });
        }
    }

    /**
     * Hide default Laravel commands from the artisan command list
     */
    protected function hideDefaultCommands(): void
    {
        // Get all registered commands
        $commands = Artisan::all();
        
        foreach ($commands as $name => $command) {
            // Keep commands in our whitelist
            if (in_array($name, $this->keepCommands)) {
                continue;
            }

            $commandClass = get_class($command);
            
            // Keep our custom commands
            if (str_starts_with($commandClass, 'App\\Console\\Commands\\')) {
                continue;
            }

            // Hide commands in specified namespaces
            foreach ($this->hideNamespaces as $namespace) {
                if (str_starts_with($name, $namespace)) {
                    $command->setHidden(true);
                    break;
                }
            }
        }
    }
}

How It Works

Let's break down the implementation:

1. Service Provider Setup

We create a dedicated ConsoleServiceProvider to handle command visibility. The logic runs in the boot() method, but only when the application is running in the console:

if ($this->app->runningInConsole()) {
    $this->app->booted(function () {
        $this->hideDefaultCommands();
    });
}

We use $this->app->booted() to ensure all commands are registered before we attempt to hide them.

2. Whitelist Approach

The $keepCommands array allows you to whitelist specific commands you want to remain visible:

protected array $keepCommands = [
    'inspire',
];

This is useful for common commands you use frequently, even if they're not custom application commands.

3. Namespace-Based Hiding

The $hideNamespaces array defines command prefixes to hide. By using namespaces, we can hide entire groups of commands with a single entry:

'make:',  // Hides make:controller, make:model, make:migration, etc.
'cache:', // Hides cache:clear, cache:forget, cache:table, etc.

4. Smart Filtering Logic

The hideDefaultCommands() method iterates through all registered commands and applies three filters:

  1. Skip whitelisted commands - Commands in $keepCommands always remain visible
  2. Keep custom commands - Any command in App\Console\Commands\ stays visible
  3. Hide by namespace - Commands matching any prefix in $hideNamespaces are hidden

Installation

To use this in your Laravel project:

  1. Create the service provider:
php artisan make:provider ConsoleServiceProvider

  1. Replace the generated content with the code above
  2. Register the provider in bootstrap/providers.php:
return [
    App\Providers\AppServiceProvider::class,
    App\Providers\ConsoleServiceProvider::class, // Add this line
];

  1. Run php artisan to see your cleaned-up command list!

Benefits

1. Improved Developer Experience

Your team can quickly see available custom commands without scrolling through dozens of Laravel defaults.

2. Better Onboarding

New developers can easily discover your application's custom commands, making it easier to learn your project's CLI interface.

3. Reduced Cognitive Load

Less clutter means less mental overhead when working with Artisan commands.

4. Commands Still Work

Hidden commands are only hidden from the list—they're still fully functional and can be executed normally:

php artisan migrate  # Still works!
php artisan cache:clear  # Still works!

Customization Tips

Show Commands You Use Frequently

If you regularly use certain Laravel commands, add them to the whitelist:

protected array $keepCommands = [
    'inspire',
    'migrate',
    'db:seed',
    'queue:work',
    'tinker',
];

Hide Package Commands

You can also hide commands from third-party packages:

protected array $hideNamespaces = [
    // ... existing entries
    'horizon:',
    'telescope:',
    'pulse:',
];

Environment-Specific Hiding

Want to show all commands in development but hide them in production?

public function boot(): void
{
    if ($this->app->runningInConsole() && $this->app->environment('production')) {
        $this->app->booted(function () {
            $this->hideDefaultCommands();
        });
    }
}

Alternative Approach: Show Only Custom Commands

If you want to go even further and only show your custom commands, you can simplify the logic:

protected function hideAllExceptCustomCommands(): void
{
    $commands = Artisan::all();
    
    foreach ($commands as $name => $command) {
        $commandClass = get_class($command);
        
        // Only show commands from App\Console\Commands
        if (!str_starts_with($commandClass, 'App\\Console\\Commands\\')) {
            $command->setHidden(true);
        }
    }
}

Conclusion

Hiding unused Artisan commands is a small quality-of-life improvement that can make a big difference in your daily development workflow. By keeping your command list focused on what matters to your application, you make it easier for your team to discover and use your custom commands.

The beauty of this approach is that it's completely non-destructive—all commands remain functional, and you can easily adjust what's visible by modifying the $keepCommands and $hideNamespaces arrays.

Give it a try in your next Laravel project and enjoy a cleaner, more focused php artisan experience!

Have you implemented command hiding in your Laravel projects? What other Artisan customizations do you find useful? Let me know in the comments below!

How was this article?

Related Posts

Comments (0)

Leave a Comment

No comments yet. Be the first to share your thoughts!