Introducing Laravel Quizzes! Play now

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:

use Illuminate\Console\ConfirmableTrait;

use ConfirmableTrait;

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

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

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.