Package

Jpkleemans Attribute Events

🔥 Fire events on attribute changes of your Eloquent model

Package

jpkleemans/attribute-events

View on GitHub →

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.

Jpkleemans Attribute Events Related Articles

Vue Lazy Image Component image

Vue Lazy Image Component

Read article
Pan - A simple, lightweight, and privacy-focused product analytics php package image

Pan - A simple, lightweight, and privacy-focused product analytics php package

Read article
Generate HTML Password Rules Attribute in Laravel 13.9.0 image

Generate HTML Password Rules Attribute in Laravel 13.9.0

Read article
Prevent Stale Database Reads in Laravel 13.38 image

Prevent Stale Database Reads in Laravel 13.38

Read article
Eloquent Refreshes: Load Generated Columns After Save image

Eloquent Refreshes: Load Generated Columns After Save

Read article
Debounced Queued Event Listeners in Laravel image

Debounced Queued Event Listeners in Laravel

Read article

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises

Pushsilver

Pushsilver is simple invoicing for freelancers and small studios — send polished invoices, automate reminders, and get paid on time without the bloat.

Pushsilver

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift