Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 679
stardust4891 Avatar asked Nov 30 '18 11:11

stardust4891


People also ask

How do I stop a function in Laravel?

You can use return or exit if you want to stop the command from within a function.

How do I exit php artisan serve in CMD?

Simply use Ctrl + C . It will come out to prompt state.

What is closure based console commands in Laravel?

The Closure Commands (aka Closure Based Routes) are defined using the Artisan::command() in routes/console. php. • The command() accepts two arguments: The command signature and a Closure which receives the command's arguments and options.

How do you make a command in Laravel?

To create a new artisan command, we can use the make:command artisan command. This command will make a new command class within the app/Console/Commands catalog. In case the directory does not exist in our laravel project, it'll be automatically made the primary time we run the artisan make:command command.


1 Answers

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;
    }

    // ...
}
like image 190
bmatovu Avatar answered Oct 11 '22 12:10

bmatovu