Event, Listener, Queue), reducing boilerplate for attribute-level event handling. Works alongside existing solutions like observers or model events but offers granularity at the attribute level.OrderStatusChanged, UserProfileUpdated). Enables explicit modeling of invariants tied to attribute transitions.$dispatchesEvents in the model. No need for middleware, base classes, or complex macros.created, updated).note:* vs. created_at:*).*) judiciously and filter noise in listeners (e.g., ignore updated_at).Model::update([...])).retrieved or replicating events for pre-update logic if needed.dispatchSync vs. dispatch)?EventServiceProvider or a dedicated testing package?searchable:updated).Order, User) to validate the pattern.class Order extends Model {
protected $dispatchesEvents = [
'status:shipped' => OrderShipped::class,
'status:cancelled' => OrderCancelled::class,
];
}
OrderShipped → send notification, update inventory).class OrderShipped implements ShouldQueue {
public function handle() {
Notification::send($this->order->customer, new ShipmentReady());
}
}
metadata:*).protected $dispatchesEvents = [
'metadata:*' => MetadataUpdated::class,
];
composer.json constraints).$dispatchesEvents to models incrementally.OrderShipped payload structure).*) may require regex or logic to filter irrelevant changes.events Artisan command to list dispatched events.class OrderShipped {
public function handle() {
\Log::debug('Order shipped:', ['old_status' => $this->order->getOriginal('status')]);
}
}
Laravel Debugbar).OrderCancelled listener timeouts).dispatchSync sparingly.UserProfileUpdated).Model::withoutEvents()).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Event listener crashes | Broken workflows (e.g., no emails) | Use ShouldQueue + retries. |
| Wildcard events fire excessively | Performance degradation | Narrow wildcards (e.g., metadata:color:*). |
| Attribute change not triggering | Silent data corruption | Add tests for critical attribute changes. |
| Queue backlog for async events | Delayed processing | Monitor queue length; scale workers. |
attribute:new_value).ShouldQueue where applicable.oldValue, newValue).How can I help you explore Laravel packages today?