jpkleemans/attribute-events
Composer 安装命令:
composer require jpkleemans/attribute-events
包简介
🔥 Fire events on attribute changes of your Eloquent model
README 文档
README
class Order extends Model { protected $dispatchesEvents = [ 'status:shipped' => OrderShipped::class, 'note:*' => OrderNoteChanged::class, ]; }
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
composer require jpkleemans/attribute-events
How to use it
Use the Kleemans\AttributeEvents trait in your model and add the attributes to the $dispatchesEvents property:
class Order extends Model { use AttributeEvents; protected $dispatchesEvents = [ 'created' => OrderPlaced::class, 'status:canceled' => OrderCanceled::class, 'note:*' => OrderNoteChanged::class, ]; }
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
$dispatchesEventsproperty, visit the Laravel Docs
Listening
Now you can subscribe to the events via the EventServiceProvider $listen array, or manually with Closure based listeners:
Event::listen(function (OrderCanceled $event) { // Restock inventory });
Or push realtime updates to your users, using Laravel's broadcasting feature:
Echo.channel('orders') .listen('OrderShipped', (event) => { // Display a notification })
JSON attributes
For attributes stored as JSON, you can use the -> operator:
protected $dispatchesEvents = [ 'payment->status:completed' => PaymentCompleted::class, ];
Accessors
For more complex state changes, you can use attributes defined by an accessor:
class Product extends Model { protected $dispatchesEvents = [ 'low_stock:true' => ProductReachedLowStock::class, ]; public function getLowStockAttribute(): bool { return $this->stock <= 3; } }
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.
jpkleemans/attribute-events 适用场景与选型建议
jpkleemans/attribute-events 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 542.44k 次下载、GitHub Stars 达 332, 最近一次更新时间为 2019 年 12 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「events」 「attributes」 「laravel」 「ddd」 「eloquent」 「domain events」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jpkleemans/attribute-events 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jpkleemans/attribute-events 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jpkleemans/attribute-events 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Библиотека для реализаций паттерна Pub/Sub
Wireless Cross-Component Communication
A powerful event calendar Tool for Laravel's Nova 5.
A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.
This Package provides some usefully console features like the attribute syntax for arguments and options, validation, auto ask and casting.
Command bus implementation: Commands and domain events
统计信息
- 总下载量: 542.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 332
- 点击次数: 21
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-16