In an upcoming Laravel release, you can prevent commands like
database migrations from accidentally running in production
environments. The next Laravel release should include the ability
to use a framework Prohibitable
trait to stop commands
from running:
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());
}
We don't know when the next version of Laravel 11.x will be
released, but we should get this feature in the next release since
it's been merged into the 11.x
branch. You can learn
more about this upcoming feature in the merged Pull Request #51376 by Jason
McCreary.
The post The Ability to Prevent Destructive Commands From Running is Coming to Laravel 11 appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.
Read more https://laravel-news.com/prevent-destructive-commands-from-running-in-laravel-11