Download the PHP package jpkleemans/attribute-events without Composer
On this page you can find all versions of the php package jpkleemans/attribute-events. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jpkleemans/attribute-events
More information about jpkleemans/attribute-events
Files in jpkleemans/attribute-events
Package attribute-events
Short Description 🔥 Fire events on attribute changes of your Eloquent model
License MIT
Homepage https://attribute.events/
Informations about the package attribute-events
Eloquent models fire several handy events throughout their lifecycle, like created
and deleted
. However, there are usually many more business meaningful events that happen during a model's life. With this library you can capture those, by mapping attribute changes to your own event classes.
Installation
How to use it
Use the Kleemans\AttributeEvents
trait in your model and add the attributes to the $dispatchesEvents
property:
The attribute events will be dispatched after the updated model is saved. Each event receives the instance of the model through its constructor.
For more info on model events and the
$dispatchesEvents
property, visit the Laravel Docs
Listening
Now you can subscribe to the events via the EventServiceProvider
$listen
array, or manually with Closure based listeners:
Or push realtime updates to your users, using Laravel's broadcasting feature:
JSON attributes
For attributes stored as JSON, you can use the ->
operator:
Accessors
For more complex state changes, you can use attributes defined by an accessor:
You can also use the new way of defining accessors introduced in Laravel 9.
Learn more
- “Decouple your Laravel code using Attribute Events” by Jan-Paul Kleemans
- Laravel Docs on Model Events
Sponsors
Thanks to Nexxtmove for sponsoring the development of this project.
Your logo or name here? Sponsor this project.
License
Code released under the MIT License.