Category: Laravel

Laravel’s Eloquent offers a bunch of default model events out of the box. Let’s see an example: From now, when we call the $message->read() method on our model instance, it won’t just update the read_at column on our model but will fire the reading and the read events as well. We can set up an event listener as it would be a default model event: Adding this code to a service provider – usually the AppServiceProvider – we are ready to listen to our model’s custom events.

This means if we register an observer class for our model, we can add the reading and read method to catch events using the observer.

However, in some cases refactoring some logic to listeners that are subscribed to custom model events can be a good refactoring and separation strategy.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Community Partners