Mail: [email protected] Phone: +1(424)231-4091

Models: Dispatchable Fields Event

The dispatchable fields event is a concept that ensures to trigger events when a model property changes. Unlike regular Model events that are dispatched when a specific action is made (save, update, deleting, etc), here we have a deep implementation that only triggers if a specific attribute of a model has changed.

The reason for this implementation was to avoid dispatching and listening to events when it's not necessary. For example, an order on NexoPOS can be updated in multiple ways. Every time the order is updated, we have default listeners that are ready to perform a specific action. Wouldn't it be better if when the customer assigned is changed, we do compute reports for the previous and recent customers? Wouldn't it be better if we could notify the driver only when the delivery status moves from "pending" to "ready-for-delivery"? That's the whole point of this feature.

Implementation

The implementation of that feature consists only of providing an array $dispatchableFieldsEvents on the model we want to dispatch the event. On that model we'll provide property and related events like this:

Now we need to define the event as it doesn't only receive the model as a parameter, but also the previous and new value of the property:

From here, you can safely listen to this event from our event listener like this: