为什么创建订单这里循环 10 次?
public static function findAvailableNo()
{
// 订单流水号前缀
$prefix = date('YmdHis');
for ($i = 0; $i < 10; $i++) {
// 随机生成 6 位的数字
$no = $prefix.str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
// 判断是否已经存在
if (!static::query()->where('no', $no)->exists()) {
return $no;
}
}
\Log::warning('find order no failed');
return false;
}
关于 LearnKu
假如由于某种原因
static::query()->where('no', $no)->exists()始终返回true,那这里就会有陷入死循环的风险,因此我们限定只试 10 次。