Description
At the moment we support binding to the Eloquent model events as the primary way of listening for events on resources. This works in the majority of cases, because the resource being created/updated/deleted etc is the only model of that class that will trigger the model events.
However, this doesn't work where the HTTP request will affect multiple models of the same class. E.g. if updating a Post
model had side-effects that cause other Post
models to be updated as well. In this scenario, binding to the Post::updated()
event will not be able to distinguish between which specific Post
model is the resource that was subject of the HTTP request.
Also, if you're using our model proxies implementation, you can't bind to events specifically for that proxy.
As such, we should add support to the Eloquent schema for a $dispatchesEvents
property that works the same as that property on the Eloquent model. I.e. it dispatches a specific event for the resource that is subject of the HTTP request. This would solve the above problem, and for proxies would mean the proxy is the object passed into the event.
It would also allow us to dispatch specific events for when relationship endpoints are used.
These events should be dispatched during the same transaction that wraps committing the model changes to the database. This is because the model events occur within the database transaction for that model, so it makes sense for these additional events to occur within our DB transaction.