The package adds macros to Illuminate\Support\Str. Laravel already has Str::uuid(), Str::orderedUuid() and Str::isUuid(), and macros cannot override existing methods, so the package's versions use different names.
Str macros
use Illuminate\Support\Str; $uuid = Str::fastUuid(); // version 4$ordered = Str::fastOrderedUuid(); // version 7 if (Str::fastIsUuid($someString)) { // valid UUID} $timeUuid = Str::timeBasedUuid(); // version 1$reordered = Str::reorderedTimeUuid(); // version 6$customUuid = Str::customUuid('data'); // version 8
All generators return strings. For name-based UUIDs the namespace defaults to Uuid::NS_DNS:
use Illuminate\Support\Str; $md5 = Str::nameUuidMd5('example.com'); // version 3$sha1 = Str::nameUuidSha1('example.com'); // version 5 // Same input, same UUID
Inspecting a UUID
use Illuminate\Support\Str; $uuid = Str::fastUuid(); Str::uuidVersion($uuid); // 4 (null when the string is not a valid UUID)Str::uuidTimestamp($uuid); // null here: only version 1, 6 and 7 UUIDs carry a timestamp (float seconds); also null when invalidStr::isNilUuid($uuid); // falseStr::nilUuid(); // '00000000-0000-0000-0000-000000000000'
Binary conversion
use Illuminate\Support\Str; $binary = Str::fastBinaryUuid(); // 16 bytes, version 4$orderedBinary = Str::fastBinaryOrderedUuid(); // 16 bytes, version 7 $bytes = Str::uuidToBinary('550e8400-e29b-41d4-a716-446655440000');$string = Str::binaryToUuid($bytes); Str::isValidBinaryUuid($bytes); // true
uuidToBinary() throws InvalidArgumentException for an invalid UUID, and binaryToUuid() throws it when the input is not exactly 16 bytes. The full list of binary macros is on Binary Storage, the SQL Server macros are on 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 7.x.
Each macro is called on this server for this render.
Str::fastUuid()- 75abfef9-bfe1-4cb9-8059-9a765806af86
Str::fastOrderedUuid()- 01a23843-418b-7006-b6f0-1975cd59b200
Str::reorderedTimeUuid()- 1f1ee87d-6b18-6a00-9078-ffdacd1d7c91
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-418b-7006-b6f0-1975cd59b200')- true
Str::uuidVersion('01a23843-418b-7006-b6f0-1975cd59b200')- 7
Str::uuidTimestamp('01a23843-418b-7006-b6f0-1975cd59b200')- 1796240261.515
Str::isNilUuid('01a23843-418b-7006-b6f0-1975cd59b200')- false
Direct library access
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->string);$version = $uuid4->version;
See the UUID library for the complete API.
Facade
The Uuid alias is registered through composer.json (extra.laravel.aliases), but in 7.0.1 it cannot resolve its root: the facade asks the container for Webpatser\Uuid\Uuid, whose constructor is protected. Call the library class directly instead.
use Webpatser\Uuid\Uuid; $uuid4 = Uuid::v4();$uuid7 = Uuid::v7();$isValid = Uuid::validate($someUuid);$nil = Uuid::nil();
Validation
Laravel's own uuid rule works with these UUIDs:
$rules = [ 'user_id' => 'required|uuid', 'session_id' => 'nullable|uuid:4',];
For a custom rule, use the library:
use Webpatser\Uuid\Uuid; $rules = [ 'custom_uuid' => [ 'required', function ($attribute, $value, $fail) { if (! Uuid::validate($value)) { $fail("The {$attribute} must be a valid UUID."); } }, ],];
Versions and methods
| Version | Str macro | Library |
|---|---|---|
| 1 | Str::timeBasedUuid() |
Uuid::generate(1) |
| 3 | Str::nameUuidMd5() |
Uuid::generate(3, $name, $namespace) |
| 4 | Str::fastUuid() |
Uuid::v4() |
| 5 | Str::nameUuidSha1() |
Uuid::generate(5, $name, $namespace) |
| 6 | Str::reorderedTimeUuid() |
Uuid::generate(6) |
| 7 | Str::fastOrderedUuid() |
Uuid::v7() |
| 8 | Str::customUuid() |
Uuid::generate(8, $data) |
Next steps
- Eloquent Models: UUID primary keys on models.
- Binary Storage: store UUIDs as 16 bytes.