承接 jpkleemans/attribute-events 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jpkleemans/attribute-events

最新稳定版本:1.5.1

Composer 安装命令:

composer require jpkleemans/attribute-events

包简介

🔥 Fire events on attribute changes of your Eloquent model

README 文档

README

Laravel Attribute Events Laravel Attribute Events

Build Status Last Updated Latest Stable Version License

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

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

Sponsors

Nexxtmove Logo

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.

统计信息

  • 总下载量: 447.16k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 331
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 331
  • Watchers: 5
  • Forks: 22
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-16