randomPart(6); $exists = DB::connection('business')->table($table)->where($column, $no)->exists(); if (!$exists) { return $no; } } // 连续撞 10 次说明随机源或并发量出了问题,静默返回可能重复的号比抛错危险得多 throw new \RuntimeException('单号生成失败,请重试'); } private function randomPart(int $length): string { $max = strlen(self::ALPHABET) - 1; $out = ''; for ($i = 0; $i < $length; $i++) { $out .= self::ALPHABET[random_int(0, $max)]; } return $out; } }