News

Prevent Destructive Commands From Running in Laravel

Published Updated
Prevent Destructive Commands From Running in Laravel image

Release updates

Never miss a Laravel release

Get an email whenever a new Laravel release lands.

As of Laravel 11.9 you can now prevent commands like database migrations from accidentally running in production environments with a Prohibitable trait:

use Illuminate\Console\Command;
use Illuminate\Console\Prohibitable;
 
class SomeDestructiveCommand extends Command
{
use Prohibitable;
}
 
// SomeDestructiveCommand::prohibit($this->app->isProduction());

The Laravel framework includes some database commands that include the Prohibitable trait, such as db:wipe, migrate:fresh, migrate:refresh, and migrate:reset:

public function boot(): void
{
FreshCommand::prohibit();
RefreshCommand::prohibit();
ResetCommand::prohibit();
WipeCommand::prohibit();
}

Using the DB Facade, you can prohibit destructive database commands built into Laravel:

// Prohibits: db:wipe, migrate:fresh, migrate:refresh, and migrate:reset
DB::prohibitDestructiveCommands($this->app->isProduction());

The prohibit() method accepts a Boolean argument that defaults to true, and you can conditionally prevent commands from running using whatever logic you need, so that you can still run them in development environments:

public function boot(): void
{
YourCommand::prohibit($this->app->isProduction());
}
Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

tinkerwell logo
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article