Pierce Gearhart
Pierce Gearhart

Reputation: 45

UUID Generate with Laravel Controller

I was searching for a laravel system that automatically generates UUIDs and found this

Str::uuid()->toString();

My only concern would be if it generated the same one more than once, does anyone know if it does this? And if so, is there a better option for generating UUIDs only once? Thanks!

Upvotes: 1

Views: 2014

Answers (1)

Aken Roberts
Aken Roberts

Reputation: 13447

TL;DR: It's very unlikely you'll get a collision.

Internally, Laravel uses the ramsey/uuid package to generate UUIDs via the Str utility class. Ramsey's package follows the RFC 4122 spec, which outlines the format and generation specs of UUIDs.

According to Wikipedia:

the number of random version-4 UUIDs which need to be generated in order to have a 50% probability of at least one collision is 2.71 quintillion
...
This number is equivalent to generating 1 billion UUIDs per second for about 85 years.

You're safe trusting that the UUID generated is unique unless you're operating at massive scale.

Upvotes: 2

Related Questions