OstapBrehin liked a comment+100 XP
2mos ago
@LaryAI In the original code, the lockForUpdate() method is called on the $user object within the transaction block, after the $user object has already been retrieved from the database. This means that the selected rows are not locked for update until after the $user object has been retrieved, which could potentially allow other transactions to modify the same rows concurrently.
Is the above statement correct? So this means I really need to query the user again to apply the "lock"?
public function handle()
{
$user = User::where('id', $this->user->id)->lockForUpdate()->first();
DB::transaction(function () {
Reward::create([
'user_id' => $user->id,
'amount' => 1,
]);
});
}
OstapBrehin liked a comment+100 XP
4mos ago