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, time-ordered (RFC 9562)$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 input (not meaningful for a v6 value generated under 8.x or earlier)Str::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, time-ordered (RFC 9562) |
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()- 91995faa-0aaf-4684-86e7-3939632c9176
Str::fastOrderedUuid()- 01a23838-7fc4-7001-b63e-a0b9b6d3ab8f
Str::reorderedTimeUuid()- 1f1ee863-2802-6780-bc22-414a9f65b353
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('01a23838-7fc4-7001-b63e-a0b9b6d3ab8f')- true
Str::uuidVersion('01a23838-7fc4-7001-b63e-a0b9b6d3ab8f')- 7
Str::uuidTimestamp('01a23838-7fc4-7001-b63e-a0b9b6d3ab8f')- 1796239556.548
Str::isNilUuid('01a23838-7fc4-7001-b63e-a0b9b6d3ab8f')- 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 and 9.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.