Laravel - Query Builder - Pessimistic Locking
The query builder also includes a few functions to help you achieve "pessimistic locking" when executing your select
statements. To execute a statement with a "shared lock", you may call the sharedLock
method. A shared lock prevents the selected rows from being modified until your transaction is committed:
DB::table('users')
->where('votes', '>', 100)
->sharedLock()
->get();
Alternatively, you may use the lockForUpdate
method. A "for update" lock prevents the selected records from being modified or from being selected with another shared lock:
DB::table('users')
->where('votes', '>', 100)
->lockForUpdate()
->get();