rap-2-h
rap-2-h

Reputation: 32038

Laravel stop events propagation

I have registered two queued event handlers on an event :

// in app/Providers/EventServiceProvider.php
//...

'App\Events\UserWasCreated' => [
    'App\Handlers\Events\RegisterInSalesForce',
    'App\Handlers\Events\SendMailToAdmin',
],

I want to run SendMailToAdmin only if RegisterInSalesForce is OK. I tried to return false in RegisterInSalesForce on failure and true on success but it does not work.

What should I do ? Did I miss something ?

Upvotes: 4

Views: 4194

Answers (3)

Marty Aghajanyan
Marty Aghajanyan

Reputation: 13389

In Laravel 5.5 you can just return false to stop propagation of an event.

From Laravel 5.5 documentation

Stopping The Propagation Of An Event

https://laravel.com/docs/5.5/events#defining-listeners

Sometimes, you may wish to stop the propagation of an event to other listeners. You may do so by returning false from your listener's handle method.

Upvotes: 1

Toni Perić
Toni Perić

Reputation: 512

You cannot do it exactly like that, but you can do a workaround for that:

in your RegisterInSalesForce event, you can fire another event (such as RegisteredInSales) and then listen to it.

class RegisterInSalesForce {

    public function handle()
    {
        // do your stuff

        if($stuff == true){
            $response = Event::fire(new RegisteredInSales);
        }

    }
}

Upvotes: 1

Keith Mifsud
Keith Mifsud

Reputation: 1618

Use a separate Event handler and add the logic to a method or else have another event "RegisteredInSalesForce" so a listener to it will dispatch the command "SendMailToAdmin".

Upvotes: 1

Related Questions