Creating a UID inside Laravel 5, how can I enhance it to drop the probability of collision?

半腔热情 提交于 2019-12-12 02:10:05

问题


Currently I am creating a UID for my data inside a controller method called randomId. I've pasted the entire method below:

public static function randomId()
{

    $id = str_random(12);

    $validator = Validator::make(
        ['calculation_uid'=>$id],
        ['calculation_uid'=>'unique:calculations,calculation_uid']
    );

    if ($validator->fails()) {
        $this->randomId();
    }

    return strtoupper($id);
}

Basically generates a 12 digit alpha-numeric code, and then that gets run through the validator to ensure it doesn't already exist; if it does exist it runs the method again.

I know I can reduce the probability of collision by increasing it to say 16 chars but I'll forever be increasing it if I ever hit the limit.

Is there a way I can (for a lack of better phrasing) make this more unique? Or is my implementation likely to be okay?

来源:https://stackoverflow.com/questions/42692766/creating-a-uid-inside-laravel-5-how-can-i-enhance-it-to-drop-the-probability-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!