diff --git a/events.md b/events.md index 00a9c6f028c..cc6d5b2c0c3 100644 --- a/events.md +++ b/events.md @@ -12,6 +12,7 @@ - [Queued Event Listeners & Database Transactions](#queued-event-listeners-and-database-transactions) - [Handling Failed Jobs](#handling-failed-jobs) - [Dispatching Events](#dispatching-events) + - [Interacting with Database Transactions](#interacting-with-database-transactions) - [Event Subscribers](#event-subscribers) - [Writing Event Subscribers](#writing-event-subscribers) - [Registering Event Subscribers](#registering-event-subscribers) @@ -414,6 +415,22 @@ If your queue connection's `after_commit` configuration option is set to `false` public $afterCommit = true; } +Alternatively, you might also implement the `ShouldHandleEventsAfterCommit` interface: + + **Note** > To learn more about working around these issues, please review the documentation regarding [queued jobs and database transactions](/docs/{{version}}/queues#jobs-and-database-transactions). @@ -532,6 +549,35 @@ To dispatch an event, you may call the static `dispatch` method on the event. Th > **Note** > When testing, it can be helpful to assert that certain events were dispatched without actually triggering their listeners. Laravel's [built-in testing helpers](#testing) make it a cinch. + +### Interacting with Database Transactions + +Whenever you're dealing with database transactions, you might want the events to only be dispatched once the transaction is committed. To do so, you can implement the `ShouldDispatchAfterCommit` interface on the Event class: + + Each database transaction's events are isolated. If a transaction fails, its events will be discarded and it will not affect the parent transaction. + ## Event Subscribers diff --git a/queues.md b/queues.md index debaa41b747..2fbc96c17bd 100644 --- a/queues.md +++ b/queues.md @@ -816,6 +816,43 @@ Likewise, if the `after_commit` configuration option is set to `true`, you may i ProcessPodcast::dispatch($podcast)->beforeCommit(); + +#### Specifying Commit Dispatch Behavior On The Job Class + +Alternatively, if you want to specify the commit dispatch behavior inside the job class, you can do so by implementing the `ShouldQueueAfterCommit` interface instead of `ShouldQueue`: + + ### Job Chaining