• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

How to Prevent Disasters in Laravel (A Critical Tip for Professional Developers)

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
Running powerful Artisan commands like php artisan migrate:fresh or migrate:wipe in the wrong environment—especially production—can completely wipe your database in seconds. To prevent such accidents, Laravel (starting from version 11.9) offers a built-in protection mechanism using the Prohibitable trait. In this tutorial, you’ll learn how to lock down destructive commands in production and avoid unintentional data loss with just a few lines of code.

Prohibitable Trait: Smart Protection Against Destructive Commands


As of Laravel 11.9, you can easily lock dangerous commands in production environments. This simple safeguard can save you from catastrophic errors.

How It Works


By adding a few lines of code to your Service Provider, you can disable certain Artisan commands based on the environment.


class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
FreshCommand::prohibit(app()->isProduction());
RefreshCommand::prohibit(app()->isProduction());
ResetCommand::prohibit(app()->isProduction());
RollbackCommand::prohibit(app()->isProduction());
WipeCommand::prohibit(app()->isProduction());
}
}

Here’s why it’s crucial:

migrate:wipe – Completely deletes all database tables (WipeCommand)
migrate:fresh – Resets the entire database (FreshCommand)
migrate:reset – Rolls back all migrations (ResetCommand)
migrate:refresh – Resets and reruns all migrations (RefreshCommand)
migrate:rollback – Reverts the last batch of migrations (RollbackCommand)

The key line is:


WipeCommand::prohibit(app()->isProduction());

This line accepts a boolean. If it returns true, the command is prohibited from running.

The expression app()->isProduction() checks your app’s environment—based on the APP_ENV value in your .env file.

A Simpler Solution


Want to handle all dangerous commands in one place? Just add the second code snippet to your Service Provider, and you’ll be protected across the board.


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


If you’re a professional Laravel developer—or even just getting started—this small tip can save you from a major disaster. Don’t wait for a close call to make this part of your standard setup.

Stay safe. Write smart code.
Laravel has your back—you just need to use the tools it offers.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу