toDateString(); $salt = static::query()->where('day', $today)->value('salt'); if (is_string($salt)) { return $salt; } static::forgetOld(); try { return static::query()->create(['day' => $today, 'salt' => Str::random(64)])->salt; } catch (UniqueConstraintViolationException) { // Another visitor arrived in the same instant and made today's key. return static::query()->where('day', $today)->sole()->salt; } } /** Deletes every key that is not today's. Returns how many. */ public static function forgetOld(): int { $deleted = static::query()->where('day', '<', now()->toDateString())->delete(); return is_int($deleted) ? $deleted : 0; } }