Attribute Events

jpkleemans/attribute-events image

Attribute Events stats

Downloads
173.2K
Stars
287
Open Issues
3
Forks
19

View on GitHub →

🔥 Fire events on attribute changes of your Eloquent model

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.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Jpkleemans Attribute Events Related Articles

The artisan dev Command in Laravel 13.16.0 image

The artisan dev Command in Laravel 13.16.0

Read article
Drag-and-Drop Sorting for Eloquent Models with Reorderable for Laravel image

Drag-and-Drop Sorting for Eloquent Models with Reorderable for Laravel

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
Queue-Wide Inspection Methods in Laravel 13.8.0 image

Queue-Wide Inspection Methods in Laravel 13.8.0

Read article
Everything new in Livewire 4 image

Everything new in Livewire 4

Read article
Creating Responsive Component Networks with Laravel Livewire's #[On] Event System image

Creating Responsive Component Networks with Laravel Livewire's #[On] Event System

Read article
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
Acquaint Softtech logo

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
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten