Closed Elshaden closed 2 years ago
Hello,
Thank you for using this package.
Observers are sort of a shortcut for attaching Event Listeners to Eloquent Model events.
The most that this package can do for you is allowing you to enable transactional behavior to all or a subset of Eloquent Events. According to the configuration, you can make all Eloquent events transacional-aware — uncommenting eloquent.*
—, or if you want a specific one, you can simply comment the corresponding entry (e.g., eloquent.created
).
However, be aware that such action will enable transactional behavior for all Eloquent Models, not only for a specific model. This may cause side effects you may not expect.
I see two alternatives to this:
Trigger a new custom event from your Observer, so you can choose which events should be transaction-aware or not.
Open a PR to this add support for selective transaction-aware behavior for Eloquent events (i.e., only eloquent.created
event of User
model should be transaction-aware). That is, for instance, updating the configuration to something like:
'transactional' => [
'App\Events',
'App\User:eloquent.created,eloquent.saved'
]
Or adding such capabilities to the EventDispatcher, by adding a new method.
$dispatcher->setEloquentTransactionalEvents(/* map with all entries, for instance */)
Hi thanks for this package It looks like exactly what I want, the only problem is I am using model observers not events, so how can I use it in an Observer Class for a given model.
the idea is created method in the observer is only fired when a commit is successful, same with updated and others
Thanks