OstapBrehin's avatar

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's avatar

OstapBrehin liked a comment+100 XP

4mos ago

I know for awhile Jeffrey was using this, I currently still use it...not sure if there's something better these days

https://github.com/erichard/SublimePHPCompanion

Click anywhere in the class name and hit F5 and it's imported