webpatser/laravel-uuid

Generate uuid with only 8 digits?

erdely opened this issue · 6 comments

Is there any possibility to create a uuid but only with 8 digits, as datatype integer?

eeh no. UUID is 128-bit value so creating an 8-bit version is kinda hard. If you need an 8-bit random integer. Just use the rand() option. But I guess this would give collisions fast :-)

I know how to create random, but not unique ;)

Anyway this plugin is very, very good, thank you!

depending why you need a random integer. If you only need it to obscure an URL (not wanting to display the ID), you can also add the UUID in a separate char(36) field. And use that in your URL's.

I need that as "customer number", this id will be printed on cards, of course must be unique. For url's I have used your solution

You could alter your auto incrementing id column to start at 10000000 or something like that and just use the id as the customer number.

Otherwise you can add a unique constraint to your db column, generate a random 8 digit string, and try/catch the insert. For 8 digits you should be looking at a few hundred thousand random numbers (I think?) before you run into real risk of collision.

you can do it this way

// With this precision (microsecond) ID will looks like '2di2adajgq6h'
$id = base_convert(microtime(false), 10, 36);

// With less precision (second) ID will looks like 'niu7pj'
$id = base_convert(time(), 10, 36);