The package gives you two ways to work with UUIDs: Str macros and the core Webpatser\Uuid\Uuid class.
Str macros
Laravel's Str::uuid(), Str::orderedUuid() and Str::isUuid() already exist and cannot be overridden by macros, so the package adds alternatives with different names. They are backed by webpatser/uuid.
use Illuminate\Support\Str; $uuid = Str::fastUuid(); // v4, returns a string$ordered = Str::fastOrderedUuid(); // v7, returns a string if (Str::fastIsUuid($someString)) { // valid UUID} $timeUuid = Str::timeBasedUuid(); // v1$reordered = Str::reorderedTimeUuid(); // v6$customUuid = Str::customUuid('data'); // v8$md5Uuid = Str::nameUuidMd5('example.com'); // v3$sha1Uuid = Str::nameUuidSha1('example.com'); // v5
The two name-based macros take a name and an optional namespace (default Uuid::NS_DNS). The same input always gives the same UUID:
// $a === $b
Inspection macros
$uuid = Str::fastUuid(); Str::uuidVersion($uuid); // 4, or null when the string is not a valid UUIDStr::uuidTimestamp($uuid); // timestamp for v1, v6 and v7, null for other versions or invalid inputStr::nilUuid(); // the nil UUIDStr::isNilUuid($uuid); // false
Macro reference
| Macro | Returns |
|---|---|
fastUuid() |
v4 string |
fastOrderedUuid() |
v7 string |
fastIsUuid(string $value) |
bool |
timeBasedUuid() |
v1 string |
reorderedTimeUuid() |
v6 string |
customUuid($data = null) |
v8 string |
nameUuidMd5(string $name, string $namespace = Uuid::NS_DNS) |
v3 string |
nameUuidSha1(string $name, string $namespace = Uuid::NS_DNS) |
v5 string |
uuidVersion(string $uuid) |
version or null |
uuidTimestamp(string $uuid) |
timestamp or null |
nilUuid() |
nil UUID string |
isNilUuid(string $uuid) |
bool |
Binary and SQL Server macros are covered in Binary Storage and SQL Server.
Try the macros
Every macro below is called on this server when the demo renders. Change the name or paste a UUID to check.
Each macro is called on this server for this render.
Str::fastUuid()- 7e60b283-a594-4f58-b94c-ad1e63e48fa9
Str::fastOrderedUuid()- 01a23843-424e-700b-8d99-7757c40d8866
Str::reorderedTimeUuid()- 1f1ee87d-6cf6-6880-a41a-1b4316cce7ba
Str::nameUuidMd5('example.com')- 9073926b-929f-31c2-abc9-fad77ae3e8eb
Str::nameUuidSha1('example.com')- cfbff0d1-9375-5685-968c-48ce8b15ae17
Str::nilUuid()- 00000000-0000-0000-0000-000000000000
Str::fastIsUuid('01a23843-424e-700b-8d99-7757c40d8866')- true
Str::uuidVersion('01a23843-424e-700b-8d99-7757c40d8866')- 7
Str::uuidTimestamp('01a23843-424e-700b-8d99-7757c40d8866')- 1796240261.71
Str::isNilUuid('01a23843-424e-700b-8d99-7757c40d8866')- false
The core Uuid class
use Webpatser\Uuid\Uuid; $uuid4 = Uuid::v4();$uuid7 = Uuid::v7();$uuid1 = Uuid::generate(1);$uuid5 = Uuid::generate(5, 'example.com', Uuid::NS_DNS); $isValid = Uuid::validate($uuid4);$version = $uuid7->version;$timestamp = $uuid7->time;
See the UUID library docs for the full API.
The Uuid facade
Warning: The
Uuidfacade alias does not resolve in 8.0.0, becauseWebpatser\Uuid\Uuidhas a protected constructor and the package binds nothing in the container. ImportWebpatser\Uuid\Uuidand call its static methods directly, as shown above.
Validation
Laravel's own uuid rule works with these UUIDs:
$rules = [ 'user_id' => 'required|uuid', 'session_id' => 'nullable|uuid:4',];
Next steps
- Eloquent Models: primary keys, casts and route model binding.
- Binary Storage: 16-byte UUID columns.