Rajesh Kumar
Rajesh Kumar

Reputation: 247

laravel events - call another event from an event

My EventServiceProvider has the following,

'App\Events\EventCreated' => [
            'App\Handlers\Events\EventCreatedNotifications',
            'App\Handlers\Events\EventCreatedMail'
        ],

I want to call the EventCreatedMail event from the EventCreatedNotifications by passing parameters, is it possible to do this.

Upvotes: 0

Views: 3061

Answers (1)

Sh1d0w
Sh1d0w

Reputation: 9520

When triggering App\Events\EventCreated event, all event classes registered for this event will be triggered. What you are asking is not good in terms of code organization. Your EventCreatedMail will be called annyways when EventCreatedNotifications is called. Yes, you can instantiate new EventCreatedMail class and call it's handle function to simulate calling the event, but this is wrong and you should not do it.

Instead you can just separate both events, and then you can easily call EventCreatedMail from where you want, like this:

'App\Events\EventCreated' => [
    'App\Handlers\Events\EventCreatedNotifications',
],
'App\Events\CreatedNotification' => [
    'App\Handlers\Events\EventCreatedMail'
],

After that you can call

event(new \App\Events\CreatedNotification($event->id, $input['notify']));

From your first event.

Upvotes: 2

Related Questions