【问题标题】:How do i generate unique 5 digit customer number with hasid如何使用 hasid 生成唯一的 5 位客户编号
【发布时间】:2017-12-27 09:55:09
【问题描述】:

嗨,我想生成 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,...等的东西

我该怎么做??

【问题讨论】:

  • 为什么不使用 10000 到 99999 之间的随机数?
  • 该 github 链接告诉您如何使用自定义“字母”...也就是 hashid 将使用的唯一字符

标签: php laravel hashids


【解决方案1】:

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

我想这样的解决方案会更好:

$idWasNotCreated = true;
while ($idWasNotCreated)
    $id = mt_rand(100000, 999999);
    if (User::where('customer_number', $id)->first()) {
        $idWasNotCreated = false;
    }
}

【讨论】:

    猜你喜欢
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    相关资源
    最近更新 更多