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

Model::dispatchableFieldsEvent

The dispatchable fields event is a property of models that allows you to dispatch a specific event when the attribute of that model changes from one state to another. This is useful as it helps trigger events at the right time.

Here is how you'll proceed to extend existing models with your custom event.

In this scenario, we're extending the default Order model and adding a custom dispatchableFieldEvent.

Declaring Event

When the order payment_status changes we'll dispatch the event "OrderAfterPaymentStatusChangedEvent". You should note that the event must have 3 properties: model, previous, and new.

  • model: it is a reference to the model being updated
  • previous: is the previous value of the changed attribute
  • new: is the current value of the changed attribute

Here is how you should declare your event.

Listening Event

Any module that wants to catch specific attribute changes needs to listen to that event. They'll use both "previous" and "new" to decide which action to perform. For example, if an order is "unpaid" and then "paid" you might consider sending the paid email. If that order is "paid" then "refunded" you might dispatch another email.

Note that, you can also still rely on the actual order status, but in that situation, every time an order is updated, you might find yourself sending the paid email each time.