~/docs
Resonate
Resonate
9.x 9.0.0, current 8.x 8.0.0 7.x 7.0.1 6.x 6.2.1 5.x 5.1.2 4.x 4.0.2
Laravel UUID
Toggle sidebar
Laravel UUID
webpatser/laravel-uuid 7.0.1
You are reading the 7.x documentation
The current line is 9.x. Read this page for 9.x

Laravel UUID 7.x

Quick Start

Generate, validate and convert UUIDs in Laravel with the Str macros and the UUID library.

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
Str::nameUuidSha1('[email protected]') === Str::nameUuidSha1('[email protected]'); // true

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 invalid
Str::isNilUuid($uuid); // false
Str::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.

Name for the name-based macros
UUID to check Empty uses the fastOrderedUuid() output.

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