如何使用hasid生成唯一的5位客户编号

问题描述 投票:1回答:1

嗨,我想生成unique五位customer number只包含来自0....9的数字

我正在使用这个包:https://github.com/ivanakimov/hashids.php

我能够生成id但它包含alphabet就像... z

这就是我的生成方式

   $id = 12;
   $hashids = new Hashids\HashIds('eabangalore');  
   return $id = $hashids->encode(1, $id, 99);    // output QBsLFJw

但我想要输出像这样的22345,46643,...等

我怎样才能做到这一点 ??

php laravel hashids
1个回答
1
投票

我认为您实际上不能使用此软件包来创建从100000到999999的真正唯一的客户编号。

我猜这样的事情会是一个更好的解决方案:

$idWasNotCreated = true;
while ($idWasNotCreated)
    $id = mt_rand(100000, 999999);
    if (User::where('customer_number', $id)->first()) {
        $idWasNotCreated = false;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.