Vivek Mistry 👋

I’m a Certified Senior Laravel Developer with 8+ years of experience , specializing in building robust APIs and admin panels, frontend templates converting them into fully functional web applications.

Book A Call
  • 13 Mar, 2026
  • 7 Views
  • Prevent dangerous operations by asking user confirmation in CLI commands.

Make Artisan Commands Interactive in Laravel Using confirm()

The Problem: Dangerous Console Commands

In real Laravel applications, some Artisan commands perform risky operations like:

  • Deleting old data
  • Clearing records
  • Running migrations
  • Resetting statistics

Running them accidentally can cause serious problems.


The Safe Solution: confirm()

Laravel provides the confirm() method to ask the user before executing a command.

Example:

if (! $this->confirm('Do you really want to delete old logs?')) {
    return;
}

If the user types no, the command stops immediately.


Real Project Example

Imagine a command that deletes logs older than 90 days.

public function handle()
{
    if (! $this->confirm('Delete logs older than 90 days?')) {
        $this->info('Operation cancelled.');
        return;
    }
    LogEntry::where('created_at', '<', now()->subDays(90))->delete();
    $this->info('Old logs deleted successfully.');
}

Now the command becomes much safer.


Why This Is Useful

confirm() helps you:

  • Prevent accidental data loss
  • Make commands interactive
  • Improve CLI safety
  • Build professional internal tools

When to Use It

Use confirmation prompts when commands:

  • Delete data
  • Modify production records
  • Perform bulk operations
  • Run irreversible actions


Share: