Skip to content

Commit 8541e73

Browse files
committed
document after commit interfaces
1 parent a7f48c4 commit 8541e73

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

broadcasting.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,20 +507,19 @@ Sometimes you want to broadcast your event only if a given condition is true. Yo
507507

508508
When broadcast events 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 event depends on these models, unexpected errors can occur when the job that broadcasts the event is processed.
509509

510-
If your queue connection's `after_commit` configuration option is set to `false`, you may still indicate that a particular broadcast event should be dispatched after all open database transactions have been committed by defining an `$afterCommit` property on the event class:
510+
If your queue connection's `after_commit` configuration option is set to `false`, you may still indicate that a particular broadcast event should be dispatched after all open database transactions have been committed by implementing the `ShouldDispatchAfterCommit` interface on the event class:
511511

512512
<?php
513513

514514
namespace App\Events;
515515

516516
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
517+
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
517518
use Illuminate\Queue\SerializesModels;
518519

519-
class ServerCreated implements ShouldBroadcast
520+
class ServerCreated implements ShouldBroadcast, ShouldDispatchAfterCommit
520521
{
521522
use SerializesModels;
522-
523-
public $afterCommit = true;
524523
}
525524

526525
> **Note**

eloquent.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,23 +1549,17 @@ Alternatively, you may list your observers within an `$observers` property of yo
15491549
<a name="observers-and-database-transactions"></a>
15501550
#### Observers & Database Transactions
15511551

1552-
When models are being created within a database transaction, you may want to instruct an observer to only execute its event handlers after the database transaction is committed. You may accomplish this by defining an `$afterCommit` property on the observer. If a database transaction is not in progress, the event handlers will execute immediately:
1552+
When models are being created within a database transaction, you may want to instruct an observer to only execute its event handlers after the database transaction is committed. You may accomplish this by implementing the `ShouldHandleEventsAfterCommit` interface on your observer. If a database transaction is not in progress, the event handlers will execute immediately:
15531553

15541554
<?php
15551555

15561556
namespace App\Observers;
15571557

15581558
use App\Models\User;
1559+
use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit;
15591560

1560-
class UserObserver
1561+
class UserObserver implements ShouldHandleEventsAfterCommit
15611562
{
1562-
/**
1563-
* Handle events after all transactions are committed.
1564-
*
1565-
* @var bool
1566-
*/
1567-
public $afterCommit = true;
1568-
15691563
/**
15701564
* Handle the User "created" event.
15711565
*/

events.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [Queued Event Listeners & Database Transactions](#queued-event-listeners-and-database-transactions)
1313
- [Handling Failed Jobs](#handling-failed-jobs)
1414
- [Dispatching Events](#dispatching-events)
15+
- [Dispatching Events After Database Transactions](#dispatching-events-after-database-transactions)
1516
- [Event Subscribers](#event-subscribers)
1617
- [Writing Event Subscribers](#writing-event-subscribers)
1718
- [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
398399

399400
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.
400401

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:
402403

403404
<?php
404405

405406
namespace App\Listeners;
406407

408+
use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit;
407409
use Illuminate\Contracts\Queue\ShouldQueue;
408410
use Illuminate\Queue\InteractsWithQueue;
409411

410-
class SendShipmentNotification implements ShouldQueue
412+
class SendShipmentNotification implements ShouldQueue, ShouldHandleEventsAfterCommit
411413
{
412414
use InteractsWithQueue;
413-
414-
public $afterCommit = true;
415415
}
416416

417417
> **Note**
@@ -532,6 +532,35 @@ To dispatch an event, you may call the static `dispatch` method on the event. Th
532532
> **Note**
533533
> 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.
534534
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+
535564
<a name="event-subscribers"></a>
536565
## Event Subscribers
537566

0 commit comments

Comments
 (0)