Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 937
rap-2-h Avatar asked Oct 21 '25 15:10

rap-2-h


2 Answers

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

    }
}
like image 102
Toni Perić Avatar answered Oct 23 '25 03:10

Toni Perić


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".

like image 43
Keith Mifsud Avatar answered Oct 23 '25 04:10

Keith Mifsud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!