Skip to content
Advertisement

how to Abort/exit/stop/terminate in laravel console command using code

Let’s say I’m coding a command. How would I stop it completely in the middle of it running?

Example:

public function handle()
{
    if (!$this->good_times) {
        $this->error('Bad times');
        $this->exit();
    }

    // continue command stuff
}

I have tried:

throw new RuntimeException('Bad times');

But that dumps a bunch of ugliness in the terminal.

Advertisement

Answer

Just use a return statement instead of throwing an exception. Like…

public function handle()
{
    if (!$this->good_times) {
        $this->error('Bad times');
        // $this->exit();
        // throw new RuntimeException('Bad times');
        return;
    }

    // ...
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement