~/docs
Resonate
Resonate
4.x 4.0.0, current 3.x 3.0.1 2.x 2.0.1 1.x 1.3.0
UUID Library
Toggle sidebar
UUID Library
webpatser/uuid 1.3.0
You are reading the 1.x documentation
The current line is 4.x. Read this page for 4.x

UUID Library 1.x

Live Examples

Interactive examples showing all UUID versions with real-time generation and validation.

Interactive examples showing all UUID versions with real-time generation. Click the buttons to generate new UUIDs!

Interactive UUID Generation & Validation

Each UUID version serves different purposes. Use the interactive tools below to generate new UUIDs and validate existing ones:

⚡ Quick Actions

This interactive experience is powered by Laravel, Livewire & PHP, using the actual UUID library for real-time generation and validation

🎲 Interactive UUID Generator

84759515-c05f-49a4-81c1-bf61458fa599
84759515c05f49a481c1bf61458fa599
84759515c05f49a481c1bf61458fa599

🔍 UUID Validator & Inspector

Enter a UUID above to validate

Usage Examples

Basic Generation

use Webpatser\Uuid\Uuid;
 
// Random UUID (most common)
$uuid = Uuid::v4();
echo $uuid; // e.g., "f47ac10b-58cc-4372-a567-0e02b2c3d479"
 
// Time-based UUID
$timeUuid = Uuid::v1();
echo $timeUuid; // e.g., "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
 
// Modern timestamp UUID (recommended for databases)
$modernUuid = Uuid::v7();
echo $modernUuid; // e.g., "018f4e7c-8b3a-7000-8000-123456789abc"

Live output

The block below generates one UUID per version on this server, each next to the call that produced it. Press Generate again for new values. The name-based rows are always the same for the same name and namespace.

Generated on this server for this page view
  • Uuid::v4() v4, random
    864e5ef5-00a5-4c73-926e-8cb4e03b259e
  • Uuid::v7() v7, time-ordered
    01a23843-382e-7005-9f66-169545d1bb52
  • Uuid::generate(1) v1, time and node
    d543e8c0-ee87-11f1-8401-2bfc58ddbfc4
  • Uuid::generate(3, 'example.com', Uuid::NS_DNS) v3, MD5 name-based
    9073926b-929f-31c2-abc9-fad77ae3e8eb
  • Uuid::generate(5, 'example.com', Uuid::NS_DNS) v5, SHA-1 name-based
    cfbff0d1-9375-5685-968c-48ce8b15ae17
  • Uuid::generate(6) v6, time-ordered (RFC 9562)
    1f1ee87d-543e-6930-8305-97c76c678ef5
  • Uuid::generate(8) v8, custom
    95e319b9-d204-8c6d-bbdf-4f4ac4ae848b
  • Uuid::nil() Nil
    00000000-0000-0000-0000-000000000000

The v3 and v5 values stay the same on every view; the others change each time.

This demo runs webpatser/uuid 3.0.0, the version installed on this site.

Name-based UUIDs

use Webpatser\Uuid\Uuid;
 
// Deterministic UUIDs from names
$uuid3 = Uuid::generate(3, 'example.com', Uuid::NS_DNS);
echo $uuid3; // Always: 9073926b-929f-31c2-abc9-fad77ae3e8eb
 
$uuid5 = Uuid::generate(5, 'example.com', Uuid::NS_DNS);
echo $uuid5; // Always: cfbff0d1-9375-5685-968c-48ce8b15ae17
 
// Custom namespace
$customNamespace = Uuid::v4();
$customUuid = Uuid::generate(5, 'my-data', $customNamespace);

Note: v3 and v5 with the same input always generate the same UUID.

Code Examples

Copy these examples to get started:

use Webpatser\Uuid\Uuid;
 
// Most common patterns
$randomUuid = Uuid::v4(); // General purpose
$timestampUuid = Uuid::v7(); // Database records
$deterministicUuid = Uuid::generate(5, $name, Uuid::NS_DNS); // From name
 
// Different formats
echo $randomUuid->string; // "123e4567-e89b-12d3-a456-426614174000"
echo $randomUuid->hex; // "123e4567e89b12d3a456426614174000"
echo bin2hex($randomUuid->bytes); // Same as hex
 
// Validation and comparison
if (Uuid::validate($input)) {
$uuid = Uuid::import($input);
if (Uuid::compare($uuid, $otherUuid)) {
echo "UUIDs match!";
}
}

Advanced Examples

Batch Generation

use Webpatser\Uuid\Uuid;
 
// Generate multiple UUIDs efficiently
$uuids = [];
for ($i = 0; $i < 1000; $i++) {
$uuids[] = Uuid::v7(); // Time-ordered for better database performance
}
 
// Custom namespace for your application
$appNamespace = Uuid::generate(5, 'myapp.com', Uuid::NS_DNS);
 
// Generate deterministic UUIDs for users
$userIds = [];
foreach (['alice', 'bob', 'charlie'] as $username) {
$userIds[$username] = Uuid::generate(5, $username, $appNamespace);
}

Database Usage

use Webpatser\Uuid\Uuid;
 
// Laravel migration example
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('email')->unique();
$table->timestamps();
});
 
// Model with UUID primary key
class User extends Model
{
protected $keyType = 'string';
public $incrementing = false;
 
protected static function boot()
{
parent::boot();
 
static::creating(function ($model) {
$model->id = Uuid::v7(); // Time-ordered for better DB performance
});
}
}