Skip to content

Commit 58c82c8

Browse files
committed
Adds mention to firing model observers after db transaction
1 parent 0e8351b commit 58c82c8

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

eloquent.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,37 @@ To register an observer, you need to call the `observe` method on the model you
12321232
User::observe(UserObserver::class);
12331233
}
12341234

1235+
When models are being created inside a database transaction, you may want to instruct an observer to only run these event handlers after the database transaction (if any) is committed:
1236+
1237+
<?php
1238+
1239+
namespace App\Observers;
1240+
1241+
use App\Models\User;
1242+
1243+
class UserObserver
1244+
{
1245+
/**
1246+
* Indicates if the model events should be fired
1247+
* after database transactions are committed.
1248+
*
1249+
* @var bool
1250+
*/
1251+
public $afterCommit = true;
1252+
1253+
/**
1254+
* Handle the User "created" event.
1255+
*
1256+
* @param \App\Models\User $user
1257+
* @return void
1258+
*/
1259+
public function created(User $user)
1260+
{
1261+
//
1262+
}
1263+
}
1264+
1265+
12351266
<a name="muting-events"></a>
12361267
### Muting Events
12371268

0 commit comments

Comments
 (0)