|
12 | 12 | - [Queued Event Listeners & Database Transactions](#queued-event-listeners-and-database-transactions)
|
13 | 13 | - [Handling Failed Jobs](#handling-failed-jobs)
|
14 | 14 | - [Dispatching Events](#dispatching-events)
|
| 15 | + - [Dispatching Events After Database Transactions](#dispatching-events-after-database-transactions) |
15 | 16 | - [Event Subscribers](#event-subscribers)
|
16 | 17 | - [Writing Event Subscribers](#writing-event-subscribers)
|
17 | 18 | - [Registering Event Subscribers](#registering-event-subscribers)
|
@@ -398,20 +399,19 @@ If you need to manually access the listener's underlying queue job's `delete` an
|
398 | 399 |
|
399 | 400 | When queued listeners are dispatched within database transactions, they may be processed by the queue before the database transaction has committed. When this happens, any updates you have made to models or database records during the database transaction may not yet be reflected in the database. In addition, any models or database records created within the transaction may not exist in the database. If your listener depends on these models, unexpected errors can occur when the job that dispatches the queued listener is processed.
|
400 | 401 |
|
401 |
| -If your queue connection's `after_commit` configuration option is set to `false`, you may still indicate that a particular queued listener should be dispatched after all open database transactions have been committed by defining an `$afterCommit` property on the listener class: |
| 402 | +If your queue connection's `after_commit` configuration option is set to `false`, you may still indicate that a particular queued listener should be dispatched after all open database transactions have been committed by implementing the `ShouldHandleEventsAfterCommit` interface on the listener class: |
402 | 403 |
|
403 | 404 | <?php
|
404 | 405 |
|
405 | 406 | namespace App\Listeners;
|
406 | 407 |
|
| 408 | + use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit; |
407 | 409 | use Illuminate\Contracts\Queue\ShouldQueue;
|
408 | 410 | use Illuminate\Queue\InteractsWithQueue;
|
409 | 411 |
|
410 |
| - class SendShipmentNotification implements ShouldQueue |
| 412 | + class SendShipmentNotification implements ShouldQueue, ShouldHandleEventsAfterCommit |
411 | 413 | {
|
412 | 414 | use InteractsWithQueue;
|
413 |
| - |
414 |
| - public $afterCommit = true; |
415 | 415 | }
|
416 | 416 |
|
417 | 417 | > **Note**
|
@@ -532,6 +532,35 @@ To dispatch an event, you may call the static `dispatch` method on the event. Th
|
532 | 532 | > **Note**
|
533 | 533 | > 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.
|
534 | 534 |
|
| 535 | +<a name="dispatching-events-after-database-transactions"></a> |
| 536 | +### Dispatching Events After Database Transactions |
| 537 | + |
| 538 | +Sometimes, you may want to instruct Laravel to only dispatch an event after the active database transaction has committed. To do so, you may implement the `ShouldDispatchAfterCommit` interface on the event class. |
| 539 | + |
| 540 | +This interface instructs Laravel to not dispatch the event until the current database transaction is committed. If the transaction fails, the event will be discarded. If no database transaction is in progress when the event is dispatched, the event will be dispatched immediately: |
| 541 | + |
| 542 | + <?php |
| 543 | + |
| 544 | + namespace App\Events; |
| 545 | + |
| 546 | + use App\Models\Order; |
| 547 | + use Illuminate\Broadcasting\InteractsWithSockets; |
| 548 | + use Illuminate\Contracts\Events\ShouldDispatchAfterCommit; |
| 549 | + use Illuminate\Foundation\Events\Dispatchable; |
| 550 | + use Illuminate\Queue\SerializesModels; |
| 551 | + |
| 552 | + class OrderShipped implements ShouldDispatchAfterCommit |
| 553 | + { |
| 554 | + use Dispatchable, InteractsWithSockets, SerializesModels; |
| 555 | + |
| 556 | + /** |
| 557 | + * Create a new event instance. |
| 558 | + */ |
| 559 | + public function __construct( |
| 560 | + public Order $order, |
| 561 | + ) {} |
| 562 | + } |
| 563 | + |
535 | 564 | <a name="event-subscribers"></a>
|
536 | 565 | ## Event Subscribers
|
537 | 566 |
|
|
0 commit comments