Anvil
Anvil - The mobile companion for Laravel Forge. Available now. Download for iOS

Snippet 1: Using Laravel's ConfirmableTrait in commands

Did you know you can easily add the production consoles messages you get with Laravel's migrate and seed to your own commands?

AR
Anthony Rappa
1 min read - 5,433 views -

Sometimes you make commands that you have can potentially run on any environment, but want to confirm the user wants to run it on production.

Laravel does this with its migrate and seed commands:

Laravel has a cool publicly available trait you can use in your commands as well.

Just import the trait:

1use Illuminate\Console\ConfirmableTrait;
2 
3use ConfirmableTrait;

Then, add the function call to the beginning of your handle method:

1public function handle(): int
2{
3 // Will only fire on production, or can pass a callback to tell it when to fire
4 if (! $this->confirmToProceed()) {
5 return 1;
6 }
7}

Hope this helps!

Read next

Introducing Anvil: The Mobile Companion for Laravel Forge

Manage your servers, deploy sites, and monitor your infrastructure from anywhere. Anvil is a native mobile app that puts the full power of Laravel Forge in your pocket - with secure API key storage, biometric protection, and one-tap deployments.

2 min read - 2,391 views -