where('code_hash', $candidateHash) ->whereNull('redeemed_at') ->where('expires_at', '>=', now()) ->exists(); if (! $exists) { $code = $candidate; $hash = $candidateHash; break; } } if (! $code || ! $hash) { $code = str_pad((string) random_int(0, $max), $length, '0', STR_PAD_LEFT); $hash = hash('sha256', $code); } $expiresAt = now()->addMinutes($expiresInMinutes); $record = PhotoboothConnectCode::query()->create([ 'event_id' => $event->getKey(), 'code_hash' => $hash, 'expires_at' => $expiresAt, ]); return [ 'code' => $code, 'record' => $record, 'expires_at' => $expiresAt, ]; } public function redeem(string $code): ?PhotoboothConnectCode { $hash = hash('sha256', $code); /** @var PhotoboothConnectCode|null $record */ $record = PhotoboothConnectCode::query() ->where('code_hash', $hash) ->whereNull('redeemed_at') ->where('expires_at', '>=', now()) ->first(); if (! $record) { return null; } $record->forceFill([ 'redeemed_at' => now(), ])->save(); return $record; } }