Skip to content

Commit f304a71

Browse files
committed
Merge branch 'missing-docs' into 8.x
2 parents 2e4596c + 09db7bd commit f304a71

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

eloquent.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ To delete a model, you may call the `delete` method on the model instance:
714714
$flight->delete();
715715

716716
You may call the `truncate` method to delete all of the model's associated database records. The `truncate` operation will also reset any auto-incrementing IDs on the model's associated table:
717-
717+
718718
Flight::truncate();
719719

720720
<a name="deleting-an-existing-model-by-its-primary-key"></a>
@@ -1232,6 +1232,38 @@ To register an observer, you need to call the `observe` method on the model you
12321232
User::observe(UserObserver::class);
12331233
}
12341234

1235+
<a name="observers-and-database-transactions"></a>
1236+
#### Observers & Database Transactions
1237+
1238+
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:
1239+
1240+
<?php
1241+
1242+
namespace App\Observers;
1243+
1244+
use App\Models\User;
1245+
1246+
class UserObserver
1247+
{
1248+
/**
1249+
* Handle events after all transactions are committed.
1250+
*
1251+
* @var bool
1252+
*/
1253+
public $afterCommit = true;
1254+
1255+
/**
1256+
* Handle the User "created" event.
1257+
*
1258+
* @param \App\Models\User $user
1259+
* @return void
1260+
*/
1261+
public function created(User $user)
1262+
{
1263+
//
1264+
}
1265+
}
1266+
12351267
<a name="muting-events"></a>
12361268
### Muting Events
12371269

0 commit comments

Comments
 (0)