~/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 8.0.0
You are reading the 8.x documentation
The current line is 9.x. Read this page for 9.x

Laravel UUID 8.x

Quick Start

Generate, validate and convert UUIDs in Laravel with the Str macros and the core Uuid class.

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

Inspection macros

$uuid = Str::fastUuid();
 
Str::uuidVersion($uuid); // 4, or null when the string is not a valid UUID
Str::uuidTimestamp($uuid); // timestamp for v1, v6 and v7, null for other versions or invalid input
Str::nilUuid(); // the nil UUID
Str::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.

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()
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 Uuid facade alias does not resolve in 8.0.0, because Webpatser\Uuid\Uuid has a protected constructor and the package binds nothing in the container. Import Webpatser\Uuid\Uuid and 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