envant / fireable
An elegant way to trigger events based on attributes changes
Installs: 585 282
Dependents: 3
Suggesters: 0
Security: 0
Stars: 448
Watchers: 9
Forks: 22
Open Issues: 2
Requires
- php: ^7.2|^8
- illuminate/database: ^5.8|^6|^7|^8|^9|^10.0
- illuminate/support: ^5.8|^6|^7|^8|^9|^10.0
Requires (Dev)
- mockery/mockery: ^1.2
- orchestra/testbench: ^3.8 || ^4 || ^5 || ^6 || ^7|^8.0
- phpunit/phpunit: ^7.5 || ^8 || ^9.5
- sempro/phpunit-pretty-print: ^1.1
README
An elegant way to trigger events based on attributes changes.
Installation
Install package through Composer
$ composer require envant/fireable
Usage
- Add the
FireableAttributes
trait to your model - Define the attributes with specified events via the
protected $fireableAttributes
property on the model
Example
Let's say we need to trigger specified events when specific model attributes are updated.
For example, you need to notify user when he gets an "approved" status. Instead of observing model's "dirty" attributes and firing events manually we could do it more elegantly by assigning specified events to attributes or even certain values of attributes.
class User extends Authenticatable { use FireableAttributes; protected $fireableAttributes = [ 'status' => [ 'approved' => UserApproved::class, 'rejected' => UserRejected::class, ], ]; }
Also you may not need to track certain values, so you can assign an event directly to an attribute itself. So, in the example below, each time the user's email is changed, the appropriate event will be fired.
class User extends Authenticatable { use FireableAttributes; protected $fireableAttributes = [ 'email' => EmailUpdated::class, ]; }
Change log
Please see the changelog for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email author email instead of using the issue tracker.
Credits
License
license. Please see the license file for more information.