How to register event listeners in laravel 5.1

It’s quite open-ended as to where to register listeners for model events. One place is in the boot method within the EventServiceProvider class:

public function boot(DispatcherContract $events)

    // Do something

Be sure to import the namespace for the DispatcherContract at the top of the file:

use Illuminate\Contracts\Bus\Dispatcher as DispatcherContract;

Eloquent models provide a method for each event that you can pass an anonymous function to. This anonymous function receives an instance of the model that you can then act upon. So if you wanted to create a URL-friendly representation of an article headline each time your Article model was saved, you can do this by listening on the saving event:

  $article->slug = Str::slug($article->headline);
Deven Rathore

Deven is an Entrepreneur, and Full-stack developer, Constantly learning and experiencing new things. He currently runs and

Published by
Deven Rathore

Recent Posts

Top 10 SEO Writing Tips for Beginners 2021

Search Engine Optimization. It’s complicated, to say the least. Search engines, like Google, are constantly…

4 days ago

Should you become a freelancer in 2021? Pros and Cons

Freelancing and working from home was long considered idyllic by many, but the global pandemic…

4 days ago

The Leading Renewable Energy Trends in 2021

The past year saw slowdowns in the ongoing shift toward renewable energy in many countries…

1 week ago

Top 20 Web Developer Portfolio templates

Are you in search for web developer portfolio templates? In this article, I have discussed…

4 weeks ago

The list of top websites to check the grammar of an essay

The hard work and dedication put into an essay should not be thrown away by…

1 month ago

Top Secure Coding Practices Based on OWASP Guidelines

When a good developer makes an app, they don’t leave behind any loose ends. They…

1 month ago