xxxxxxxxxx
DB::table('users')->where('votes', '>', 100)->sharedLock()->get();
xxxxxxxxxx
try {
app('db')->transaction(function () use ($rec_id) {
$record = Record::where('id', $rec_id)->lockForUpdate()->first();
// do great things
}
} catch (\Exception $e) {
// display an error to user
}
xxxxxxxxxx
public function index() {
return dd(\DB::transaction(function() {
if (\Auth::guard('user')->check()) {
$model = \App\Models\User::lockForUpdate()->find(1);
sleep(30);
$model->point = 100000;
$model->save();
} else {
$model = \App\Models\User::lockForUpdate()->find(1);
$model->point = $model->point + 1;
$model->save();
}
return $model;
}));
}