Add Event and Event listener to Laravel
If you need to create an event and its associated event listeners, here are the involved files:
- Create email blade template in views\mails (Can access all the data array by its keys as variables).
- Event (Add any number of params as you want) and make them as class variables so that they can be available to listeners later.
- Listener (Will have access to the $event as aparameter, and it will surely have access to all the event parameters)(Add as many listeners as you want, all of them will run when the event occurs)
- In the Listener, we may send an email message. If so, then we will have to create app\Mail\BillEventChangeNotificationMail.php which can accept $data bypassed from the listener, which in turn was bypassed by the event.
- We can then from within the Mail file access the markdown and bypass this $dataArray
- Then attach events and listeners in the eventServiceProvider in app\Providers.