The package registers macros on Illuminate\Support\Str. They wrap the static methods of Webpatser\Uuid\Uuid from the core library.
Generate UUIDs
use Illuminate\Support\Str; $uuid = Str::fastUuid(); // version 4 (random)$ordered = Str::fastOrderedUuid(); // version 7 (time-ordered) if (Str::fastIsUuid($someString)) { // valid UUID string} $timeUuid = Str::timeBasedUuid(); // version 1$reordered = Str::reorderedTimeUuid(); // version 6$nameUuid = Str::nameUuidSha1('example.com'); // version 5, DNS namespace$md5Uuid = Str::nameUuidMd5('example.com'); // version 3, DNS namespace$customUuid = Str::customUuid('data'); // version 8
nameUuidSha1() and nameUuidMd5() take a name and an optional namespace (default Uuid::NS_DNS). The same input always gives the same UUID.
Use the core class directly
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((string) $uuid4); echo $uuid7->string; // UUID as a stringecho $uuid7->version; // 7echo $uuid7->time; // Unix timestamp (float) for versions 1, 6 and 7, null otherwise
Inspect and convert
use Illuminate\Support\Str; $uuid = Str::fastUuid(); Str::uuidVersion($uuid); // 4, or null when the string is not a valid UUIDStr::uuidTimestamp($uuid); // null for version 4; a float for versions 1, 6 and 7Str::nilUuid(); // '00000000-0000-0000-0000-000000000000'Str::isNilUuid($uuid); // false $binary = Str::uuidToBinary($uuid); // 16 bytesStr::binaryToUuid($binary); // back to the string form
Macro reference
| Macro | Returns |
|---|---|
Str::fastUuid() |
Version 4 string |
Str::fastOrderedUuid() |
Version 7 string |
Str::fastIsUuid($value) |
bool |
Str::timeBasedUuid() |
Version 1 string |
Str::reorderedTimeUuid() |
Version 6 string |
Str::customUuid($data = null) |
Version 8 string |
Str::nameUuidMd5($name, $namespace = Uuid::NS_DNS) |
Version 3 string |
Str::nameUuidSha1($name, $namespace = Uuid::NS_DNS) |
Version 5 string |
Str::uuidVersion($uuid) |
Version number, or null for an invalid UUID |
Str::uuidTimestamp($uuid) |
Timestamp, or null for an invalid UUID or a version without one |
Str::nilUuid(), Str::isNilUuid($uuid) |
Nil UUID string, bool |
The binary macros are covered in Binary Storage and the SQL Server macros in SQL Server.
Try the macros
Every macro below is called on this server when the demo renders. This site runs laravel-uuid 8.0.0, and each macro shown exists with the same behaviour in 6.x.
Each macro is called on this server for this render.
Str::fastUuid()- 7dd890df-fe0e-499f-a431-9ecaa83644fc
Str::fastOrderedUuid()- 01a23843-41eb-700c-8773-7e6d018387f2
Str::reorderedTimeUuid()- 1f1ee87d-6c03-6130-94cb-bfee0946ebbe
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-41eb-700c-8773-7e6d018387f2')- true
Str::uuidVersion('01a23843-41eb-700c-8773-7e6d018387f2')- 7
Str::uuidTimestamp('01a23843-41eb-700c-8773-7e6d018387f2')- 1796240261.611
Str::isNilUuid('01a23843-41eb-700c-8773-7e6d018387f2')- false
Validation
Laravel's own uuid rule works with these UUIDs. To validate with the core library, use a closure:
use Webpatser\Uuid\Uuid; $rules = [ 'user_id' => 'required|uuid', 'custom_uuid' => [ 'required', function ($attribute, $value, $fail) { if (! Uuid::validate($value)) { $fail("The {$attribute} must be a valid UUID."); } }, ],];
Deterministic UUIDs
use Illuminate\Support\Str;use Webpatser\Uuid\Uuid; $appUuid = Uuid::generate(5, 'my-app-data', Uuid::NS_URL);
Next steps
- Eloquent Models: UUID primary keys on models.
- Binary Storage: store UUIDs as 16 bytes.
- Core UUID documentation: every UUID version and method.