This post is more than a year old. There is a chance the content is outdated.

Snippet 1: Using Laravel's ConfirmableTrait in commands

Snippet 1: Using Laravel's ConfirmableTrait in commands

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;
3use ConfirmableTrait;

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

1public function handle(): int
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 }

Hope this helps!

Anthony Rappa

By Anthony Rappa

Hello! I'm a full stack developer from Long Island, New York. Working mainly with Laravel, Tailwind, Livewire, and Alpine.js (TALL Stack). I share everything I know about these tools and more, as well as any useful resources I find from the community. You can find me on GitHub and LinkedIn.