#development #laravel #php #terminal

It's very common to have some kind of install commands if you're building a package, or have an extra command to generate some test data. You probably only want to run this command once. After it ran, it should never be called anymore.

There is a setHidden method, that makes it possible to hide a specific command.

Let's say we have a command that generates random data. We can check if the data exists and hide the command if it does. Once hidden is set to true the command won't show up when you run php artisan.

 1namespace App\Console\Commands;
 2
 3use Illuminate\Console\Command;
 4
 5class GenerateTestData extends Command
 6{
 7    protected $signature = 'app:generate-data';
 8
 9        public function __construct()
10    {
11        parent::__construct();
12        if (User::count() >= 5)) {
13            $this->setHidden(true);
14        }
15    }
16
17    public function handle()
18    {
19        User::factory()->count(5)->create();
20    }
21}

You can also do the same with the hidden property in your class definition:

1class DestructiveCommand extends Command
2{
3    protected $signature = 'db:resetdb';
4    protected $description = 'DESTRUCTIVE! do not run this unless you know what you are doing';
5
6    // Hide this from the console list.
7    protected $hidden = true;
8}