我如何在laravel中生成唯一ID?

问题描述 投票:0回答:2

我正在与Laravel的毕业设计一起工作,并希望生成小的唯一ID“最大9个字符” ...我不需要UUID,因为这会生成太长的36个字符。

php laravel id
2个回答
0
投票
您可以这样使用PHP函数:

function unique_code($limit) { return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $limit); } echo unique_code(9);

输出看起来像:

s5s108dfc


0
投票
您可以使用此库生成随机字符串:

使用照亮\ Support \ Str;

$ id = Str :: random(9);

© www.soinside.com 2019 - 2024. All rights reserved.