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

Laravel UUID 6.x

Quick Start

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

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 string
echo $uuid7->version; // 7
echo $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 UUID
Str::uuidTimestamp($uuid); // null for version 4; a float for versions 1, 6 and 7
Str::nilUuid(); // '00000000-0000-0000-0000-000000000000'
Str::isNilUuid($uuid); // false
 
$binary = Str::uuidToBinary($uuid); // 16 bytes
Str::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.

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()
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;
 
$userId = Str::nameUuidSha1('[email protected]');
$sameUserId = Str::nameUuidSha1('[email protected]'); // identical to $userId
 
$appUuid = Uuid::generate(5, 'my-app-data', Uuid::NS_URL);

Next steps