Add Event and Event listener to Laravel

560 0 0 0

Last Updated : 2024-07-16 15:15:02

If you need to create an event and its associated event listeners, here are the involved files:


  1. Create email blade template in views\mails (Can access all the data array by its keys as variables).
    Ex: resources\views\emails\userNotifications\BillEventChangeNotificationMail.blade.php

  2. Event (Add any number of params as you want) and make them as class variables so that they can be available to listeners later.
    EX: app\Events\EventNewPhaseAdded.php

  3. 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)
    EX: app\Listeners\ListenerBillPhaseChange.php

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

  5. We can then from within the Mail file access the markdown and bypass this $dataArray

  6. Then attach events and listeners in the eventServiceProvider in app\Providers.

Mohammed Anwar

Mohammed Anwar

Experienced technical lead PHP, MySQL and Laravel Developer for 15+ years, with proven ability to develop and create high-quality and optimized web applications. Great ability to build and optimize database design, schema and queries. Versed programing trainer and instructor delivering web courses and helping others to get into the field in a timely manner. Fast and eager learner for new technologies .