How do I add a custom event to a Service Provider?

Laravel events provide an observer implementation, allowing you to listen and observe for specific tasks happening in your application. Adding a custom event to a ServiceProvider means that the application will listen to it as soon as it occurs. In terms of choosing the right ServiceProvider for the job, it breaks down into two options: a Module ServiceProvider, or any of the Providers listed under app/Providers of our shop’s root directory.

Event directory:


Listener directory:


The ServiceProvider class has a $listen property, to which we simply add on the Event that we wish to add, alongside its listener.

Articles in this section

Was this article helpful?
0 out of 0 found this helpful