Everyone who uses CLI has this fear of running commands on production which were only supposed to run on local development. Commands like migrate:fresh, reset, refresh, wipe which resets the Database to its base state, etc. Very useful in development, disaster in production.

What is the Prohibitable Trait?

As the name suggests, prohibitable trait prohibits the execution of these destructive commands in a production environment. Jason McCreary added it in Laravel 11.

Here is the link to PR

How to Use

This can be used in the following two ways.

For Default DB commands

For blocking default DB commands in the production environment, we have to add the following line of code in app/Providers/AppServiceProvider.php.

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

This will stop the execution of the DB commands on production.

> php artisan migrate:fresh

   WARN  This command is prohibited from running in this environment.

For Custom Commands

For your custom commands, you have to add the trait in the command itself first.

use Illuminate\Console\Prohibitable;

class DestructiveCommand extends Command
{
    use Prohibitable;

......

Once this is done, you have to add this command to app/Providers/AppServiceProvider.php.

DestructiveCommand::prohibit($this->app->isProduction());

This will stop the execution of your destructive command on production too.

More from laravel 11 hidden gems.