Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Prohibit running destructive DB commands in Laravel

It’s always a good idea to follow best practices when it comes to software development. One of the best practices is to restrict the running of destructive commands on your servers, intentionally or unintentionally.

Database commands, for instance, are destructive commands which can cause data loss or corruption. So, we should better restrict running them on our servers. Especially on a production server.

Laravel got us covered in this regard. I recently learned about the DB::prohibitDestructiveCommands() method which lets us prohibit running destructive commands on our servers.

Here’s how we can use it to restrict the destructive commands in production environments.

namespace App\Providers;

use Illuminate\Support\Facades\DB;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        DB::prohibitDestructiveCommands(
            $this->app->isProduction()
        );
    }
}

This command restricts running the following commands.

  • db:wipe
  • migrate:fresh
  • migrate:refresh
  • migrate:reset

And if you run one of these commands, it will throw the warning message: “This command is prohibited from running in this environment.”

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?