百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

attribute-events

> 后端框架
开源

关于您语言模型属性变化的火灾事件

332 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

关于您语言模型属性变化的火灾事件

php
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

bash
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:

php
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 $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:

php
Event::listen(function (OrderCanceled $event) {
    // Restock inventory
});

Or push realtime updates to your users, using Laravel's broadcasting feature:

javascript
Echo.channel('orders')
    .listen('OrderShipped', (event) => {
        // Display a notification
    })

JSON attributes

For attributes stored as JSON, you can use the -> operator:

php
protected $dispatchesEvents = [
    'payment->status:completed' => PaymentCompleted::class,
];

Accessors

For more complex state changes, you can use attributes defined by an accessor:

php
class Product extends Model
{
    protected $dispatchesEvents = [
        'low_stock:true' => ProductReachedLowStock::class,
    ];

    public function getLowStockAttribute(): bool
    {
        return $this->stock  You can also use the [new way of defining accessors](https://laravel.com/docs/9.x/releases#eloquent-accessors-and-mutators) introduced in Laravel 9.

## Learn more
- [“Decouple your Laravel code using Attribute Events”](https://jpkleemans.medium.com/decouple-your-laravel-code-using-attribute-events-de8f2528f46a) by Jan-Paul Kleemans
- [Laravel Docs on Model Events](https://laravel.com/docs/eloquent#events)

## Sponsors

  

Thanks to Nexxtmove for sponsoring the development of this project.  
Your logo or name here? [Sponsor this project](https://github.com/sponsors/jpkleemans).

## License

Code released under the [MIT License](https://github.com/jpkleemans/attribute-events/blob/master/LICENSE).

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPaccessorsattributesddddomain-driven-design

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架