webpatser/laravel-uuid

Laravel integration for webpatser/uuid - High-performance drop-in UUID replacements (15% faster than Ramsey). Provides Str macros, HasUuids trait, facades, and casts. RFC 4122/9562 compliant.


Keywords
performance, uuid, laravel, facade, macro, rfc4122, replacement, cast, ramsey, rfc9562, hasuuids, laravel-uuid, laravel-validation, php
License
MIT

Documentation

Laravel UUID Integration

Total Downloads Latest Stable Version Licence

High-performance UUID integration for Laravel - Drop-in replacement components using the blazing fast webpatser/uuid library that's 15% faster than Ramsey UUID.

πŸ”„ Version 6.0 - Major Architecture Change

Previous versions (≀5.x): Standalone UUID implementation
Version 6.0+: Laravel integration layer using separate webpatser/uuid core library

What Changed in v6.0

  • πŸ—οΈ Split Architecture: UUID logic moved to dedicated webpatser/uuid package
  • πŸš€ Performance Focus: Laravel-specific optimizations for 15% speed boost
  • πŸ”§ Drop-in Replacements: High-performance alternatives to Laravel's UUID components
  • πŸ“¦ Clean Separation: Pure PHP UUID library + Laravel integration layer

⚑ Core UUID Library

This package integrates with webpatser/uuid - a blazing fast pure PHP UUID library that's 15% faster than Ramsey UUID with modern PHP 8.2+ optimizations.

For core UUID functionality and documentation, see: webpatser/uuid

🎯 Laravel Integration Features

Version 6.0 provides drop-in performance replacements:

  • βœ… High-Performance Str Macros - fastUuid(), fastOrderedUuid() (15-25% faster)
  • βœ… HasUuids Trait Replacement - Drop-in replacement using our library
  • βœ… Global UUID Facade - Direct access to webpatser/uuid functionality
  • βœ… Eloquent UUID Casts - Automatic UUID column casting
  • βœ… Laravel Validation Compatible - Works with existing uuid rules
  • βœ… Auto-Discovery Support - Zero-configuration setup
  • βœ… Laravel 11.x & 12.x - Latest Laravel support

Performance vs Laravel's Built-ins

Laravel Method Our Method Performance Gain
Str::uuid() Str::fastUuid() 15% faster
Str::orderedUuid() Str::fastOrderedUuid() 25% faster
Laravel's HasUuids Our HasUuids 15% faster
Ramsey validation Str::fastIsUuid() Faster validation
String UUID storage Binary UUID storage 55% storage savings
CHAR(36) columns BINARY(16) columns Better DB performance

πŸ“¦ Installation

composer require webpatser/laravel-uuid

Requirements:

  • PHP 8.2+
  • Laravel 11.x or 12.x

πŸ†• What's New in v6.0

  • πŸ—οΈ Split Architecture: Core UUID library separated from Laravel integration
  • ⚑ High-Performance Str Macros: fastUuid(), fastOrderedUuid() - 15-25% faster
  • πŸ”„ HasUuids Replacement: Drop-in trait replacement using our library
  • πŸ—ƒοΈ Binary UUID Storage: 55% smaller database storage with automatic conversion
  • 🎯 Laravel-First: Focused on Laravel ecosystem integration
  • πŸ“¦ Clean Dependencies: Uses dedicated webpatser/uuid package
  • πŸ§ͺ Production Tested: Comprehensive testing on MariaDB, MySQL, PostgreSQL, SQLite

βœ… Production Ready

v6.0.0 is thoroughly tested and production-ready:

  • βœ… MariaDB/MySQL: Tested with varbinary(16) and uuid columns
  • βœ… PostgreSQL: Support for bytea binary storage
  • βœ… SQLite: Compatible with BLOB binary columns
  • βœ… Route Model Binding: Seamless URL-to-model resolution
  • βœ… Foreign Keys: Both binary and string UUID relationships
  • βœ… Migration Helpers: Database-specific column types
  • βœ… Performance: 55% storage savings with binary UUIDs
  • βœ… Comprehensive Tests: 10 test suites, 67 assertions passing

πŸš€ Quick Usage

Option 1: High-Performance Laravel Macros (Recommended)

use Illuminate\Support\Str;

$uuid = Str::fastUuid();                // 15% faster than Str::uuid()
$ordered = Str::fastOrderedUuid();      // 25% faster than Str::orderedUuid() 
$valid = Str::fastIsUuid($uuid);        // Fast validation

// All UUID versions available
$timeUuid = Str::timeBasedUuid();       // V1 time-based
$nameUuid = Str::nameUuidSha1('domain'); // V5 name-based

Option 2: Direct Library Access (Maximum Performance)

use Webpatser\Uuid\Uuid;

$uuid4 = Uuid::v4();                    // Random UUID
$uuid7 = Uuid::v7();                    // Database-optimized
$valid = Uuid::validate($uuid);         // Validate any UUID

// Laravel Facade (auto-registered)
use Uuid; // Global facade
$uuid = Uuid::v4();

Option 3: Eloquent Model Integration

use Webpatser\LaravelUuid\HasUuids;

class User extends Model 
{
    use HasUuids; // 15% faster than Laravel's HasUuids
}

Option 4: Binary UUID Storage (55% Storage Savings)

use Webpatser\LaravelUuid\HasBinaryUuids;
use Webpatser\LaravelUuid\BinaryUuidCast;

class User extends Model 
{
    use HasBinaryUuids; // Stores UUIDs as 16 bytes instead of 36 chars
    
    protected $casts = [
        'id' => BinaryUuidCast::class,
        'parent_id' => BinaryUuidCast::class,
    ];
}

// Migration: Use binary columns instead of string
Schema::create('users', function (Blueprint $table) {
    $table->binary('id', 16)->primary();           // 16 bytes vs 36 chars
    $table->binary('parent_id', 16)->nullable();   // 55% storage savings
    $table->string('name');
});

πŸ”§ Laravel Integration Features

UUID Validation

Laravel has built-in UUID validation that works perfectly with this library:

// Use Laravel's built-in UUID validation rules
$rules = [
    'identifier' => 'required|uuid',        // Any valid UUID
    'user_uuid' => 'nullable|uuid:4',       // Specific UUID version
    'legacy_id' => 'uuid:1',                // Version 1 UUIDs only
];

// For custom validation, use our UUID library directly
use Webpatser\Uuid\Uuid;

$rules = [
    'custom_uuid' => [
        'required',
        function ($attribute, $value, $fail) {
            if (!Uuid::validate($value)) {
                $fail("The {$attribute} must be a valid UUID.");
            }
        },
    ],
];

Service Provider

The UuidServiceProvider is automatically discovered by Laravel and provides:

  • Adds high-performance Str UUID methods (fastUuid(), fastOrderedUuid(), etc.)
  • Sets up the global Uuid facade (Webpatser\LaravelUuid\UuidFacade)
  • Enables Eloquent model UUID casting (Webpatser\LaravelUuid\UuidCast)

No manual registration needed - everything works out of the box!

Global Facade

The Uuid facade (Webpatser\LaravelUuid\UuidFacade) is automatically registered and provides access to all UUID functionality:

// Via facade (auto-imported)
use Uuid;

$uuid4 = Uuid::v4();                    // Generate V4 UUID
$uuid7 = Uuid::v7();                    // Generate V7 UUID  
$uuid = Uuid::generate(1);              // Generate any version
$isValid = Uuid::validate($uuid);       // Validate UUIDs
$nil = Uuid::nil();                     // Get nil UUID

// Or via full namespace (bypasses facade)
use Webpatser\Uuid\Uuid as DirectUuid;
$uuid = DirectUuid::v7();

Eloquent Model Casts

Automatically cast UUID columns in your Eloquent models:

use Illuminate\Database\Eloquent\Model;
use Webpatser\LaravelUuid\UuidCast;

class User extends Model
{
    protected $casts = [
        'id' => UuidCast::class,
        'uuid' => UuidCast::class,
        'parent_id' => UuidCast::class,
    ];
}

// Usage - automatically converts strings to UUID objects
$user = User::find('550e8400-e29b-41d4-a716-446655440000');
$userId = $user->id; // Returns Webpatser\Uuid\Uuid instance
echo $userId->version; // 4
echo $userId->string;  // "550e8400-e29b-41d4-a716-446655440000"

// When saving, UUID objects are automatically converted to strings
$user->uuid = Uuid::v7();
$user->save(); // Stores as string in database

Extended Str UUID Methods

This package adds high-performance UUID methods to Laravel's Str class:

// High-performance UUID methods (use these for best performance)
$uuid = Str::fastUuid();                // V4 UUID (15% faster than Str::uuid())
$ordered = Str::fastOrderedUuid();      // V7 UUID (25% faster than Str::orderedUuid())
$isValid = Str::fastIsUuid($uuid);      // Validation (faster than Str::isUuid())

// New UUID functions available:
$timeUuid = Str::timeBasedUuid();       // V1 time-based
$reorderedTime = Str::reorderedTimeUuid(); // V6 reordered time
$custom = Str::customUuid('data');      // V8 custom
$nameUuid = Str::nameUuidSha1('example.com'); // V5 name-based

// Utility functions:
$version = Str::uuidVersion($uuid);     // Get UUID version (1-8)
$timestamp = Str::uuidTimestamp($uuid); // Extract timestamp (V1/V6/V7)
$nil = Str::nilUuid();                  // Nil UUID
$isNil = Str::isNilUuid($uuid);        // Check if nil

// SQL Server GUID conversion (zero performance impact)
$sqlServerGuid = Str::uuidFromSqlServer('825B076B-44EC-E511-80DC-00155D0ABC54');
$toSqlServer = Str::uuidToSqlServer($uuid);           // Export to SQL Server format
$sqlServerBinary = Str::uuidToSqlServerBinary($uuid); // SQL Server binary format
$isSqlServer = Str::isSqlServerGuid($someGuid);       // Heuristic detection

Use Str::fastUuid() instead of Str::uuid() for 15% better performance!

High-Performance HasUuids Trait

Drop-in replacement for Laravel's HasUuids trait with 15% better performance:

use Illuminate\Database\Eloquent\Model;
use Webpatser\LaravelUuid\HasUuids; // Instead of Laravel's trait

class User extends Model
{
    use HasUuids; // Uses our high-performance library
    
    // Optional: Customize UUID generation
    public function newUniqueId(): string
    {
        return (string) Uuid::v4(); // or v7(), generate(1), etc.
    }
    
    // Optional: Specify UUID columns
    public function uniqueIds(): array
    {
        return ['id', 'uuid', 'external_id'];
    }
}

// Usage - same API as Laravel's trait
$user = User::create(['name' => 'John']);
echo $user->id; // Generated with our fast UUID library

// Additional methods available:
echo $user->getUuidVersion();      // Get UUID version
$isOrdered = $user->usesOrderedUuids(); // Check if V7 UUIDs
$timestamp = $user->getUuidTimestamp();  // Get timestamp from UUID

πŸ—ƒοΈ Binary UUID Storage (NEW in v6.0)

55% storage savings by storing UUIDs as 16-byte binary data instead of 36-character strings:

use Webpatser\LaravelUuid\HasBinaryUuids;
use Webpatser\LaravelUuid\BinaryUuidCast;
use Webpatser\LaravelUuid\BinaryUuidMigrations;

class User extends Model
{
    use HasBinaryUuids; // Automatic binary UUID support
    
    protected $casts = [
        'id' => BinaryUuidCast::class,        // Auto-converts binary ↔ UUID objects
        'parent_id' => BinaryUuidCast::class, // Handles nullable columns
    ];
}

// Migration with helper methods
Schema::create('users', function (Blueprint $table) {
    BinaryUuidMigrations::addBinaryUuidPrimary($table);  // 16-byte primary key
    BinaryUuidMigrations::addBinaryUuidColumn($table, 'parent_id', true); // nullable
    $table->string('name');
});

// Usage - works exactly like string UUIDs
$user = User::create(['name' => 'John']);
echo $user->id;                    // Displays as string: "123e4567-e89b-..."
echo $user->getUuidAsString();     // Explicit string conversion
$user->setUuidFromString($uuid);   // Set from string UUID

// Route model binding works automatically
Route::get('/user/{user}', function (User $user) {
    // Accepts string UUID in URL, finds binary UUID in database
});

// Binary UUID Str macros for direct usage
$binaryUuid = Str::fastBinaryUuid();             // 16 bytes vs 36 chars  
$orderedBinary = Str::fastBinaryOrderedUuid();   // V7 binary for databases
$stringUuid = Str::binaryToUuid($binaryUuid);    // Convert to string
$backToBinary = Str::uuidToBinary($stringUuid);  // Convert to binary

Benefits of Binary Storage:

  • βœ… 55% smaller storage (16 bytes vs 36 bytes per UUID)
  • βœ… Faster database queries and indexing
  • βœ… Better memory usage in large datasets
  • βœ… Improved clustering for V7 UUIDs in databases
  • βœ… Transparent conversion - works like string UUIDs in your code

Database-Specific Implementation:

Database Column Type Laravel Migration Actual Storage Test Status
MySQL/MariaDB varbinary(16) BinaryUuidMigrations::addBinaryUuidPrimary($table) 16 bytes βœ… Tested
PostgreSQL bytea BinaryUuidMigrations::addBinaryUuidPrimary($table) 16 bytes βœ… Supported
SQLite BLOB BinaryUuidMigrations::addBinaryUuidPrimary($table) 16 bytes βœ… Supported
SQL Server uniqueidentifier BinaryUuidMigrations::addBinaryUuidPrimary($table) 16 bytes βœ… GUID Support

Real-World MariaDB Test Results:

  • Binary UUIDs: varbinary(16) β†’ 16 bytes storage
  • String UUIDs: uuid β†’ 36 bytes storage
  • Storage Savings: 20 bytes per UUID (55.6% reduction)
  • Query Performance: Comparable speed, better scalability
  • Route Model Binding: Seamless URL string β†’ binary lookup

Migration Examples:

-- MySQL/MariaDB
CREATE TABLE users (
    id BINARY(16) PRIMARY KEY,         -- 16 bytes (55% savings!)
    parent_id BINARY(16) NULL          -- 16 bytes  
);

-- PostgreSQL
CREATE TABLE users (
    id bytea PRIMARY KEY,              -- 16 bytes (55% savings!)
    parent_id bytea NULL               -- 16 bytes
);

-- SQLite  
CREATE TABLE users (
    id BLOB PRIMARY KEY,               -- 16 bytes (55% savings!)
    parent_id BLOB NULL                -- 16 bytes
);

Automatic Database Detection:

// The package automatically detects your database and uses optimal types
BinaryUuidMigrations::addBinaryUuidPrimary($table);   // Uses correct type for your DB
$info = BinaryUuidMigrations::getDatabaseInfo();      // Get DB-specific details
$sql = BinaryUuidMigrations::getConversionSql('users', 'id'); // DB-specific conversion

πŸ—ƒοΈ SQL Server GUID Support

Handle SQL Server's mixed-endianness GUID format seamlessly:

use Illuminate\Support\Str;

// Import UUID from SQL Server with automatic byte order correction
$sqlServerGuid = '825B076B-44EC-E511-80DC-00155D0ABC54';  // From SQL Server
$correctedUuid = Str::uuidFromSqlServer($sqlServerGuid);   // Standard format
echo $correctedUuid; // '6B075B82-EC44-11E5-80DC-00155D0ABC54'

// Export UUID to SQL Server format
$standardUuid = '6B075B82-EC44-11E5-80DC-00155D0ABC54';
$sqlServerFormat = Str::uuidToSqlServer($standardUuid);    // For SQL Server
echo $sqlServerFormat; // '825B076B-44EC-E511-80DC-00155D0ABC54'

// Binary SQL Server GUID handling
$sqlServerBinary = Str::uuidToSqlServerBinary($standardUuid);     // 16-byte SQL Server format
$backToString = Str::sqlServerBinaryToUuid($sqlServerBinary);     // Convert back to standard

// Automatic format detection (optional)
if (Str::isSqlServerGuid($someGuid)) {
    $corrected = Str::uuidFromSqlServer($someGuid);
}

Laravel Migration Support

SQL Server uniqueidentifier columns are automatically supported:

use Webpatser\LaravelUuid\BinaryUuidMigrations;

Schema::create('users', function (Blueprint $table) {
    // Automatically uses 'uniqueidentifier' on SQL Server
    BinaryUuidMigrations::addBinaryUuidPrimary($table);
    BinaryUuidMigrations::addBinaryUuidColumn($table, 'parent_id', true);
    $table->string('name');
});

Database Support Matrix

Database Column Type Storage SQL Server GUID Support
MySQL/MariaDB varbinary(16) 16 bytes βœ… Via macros
PostgreSQL bytea 16 bytes βœ… Via macros
SQLite BLOB 16 bytes βœ… Via macros
SQL Server uniqueidentifier 16 bytes βœ… Native + macros

Benefits:

  • βœ… Automatic endianness handling for SQL Server
  • βœ… Cross-database compatibility with conversion macros
  • βœ… Transparent round-trip conversion (lossless)
  • βœ… Zero performance impact (only when using SQL Server methods)

πŸ“Š Performance

All UUID generation performance comes from the underlying webpatser/uuid library:

  • Version 7: ~500,000+ UUIDs/second
  • Version 4: ~700,000+ UUIDs/second
  • 15.1% faster than Ramsey UUID
  • 25.7% faster for V7 generation

πŸ”„ Migration Guide

From v5.x to v6.0 (Architecture Change)

Major Change: v6.0 splits into two packages for cleaner architecture.

Before v6.0: Single package with everything
v6.0+: webpatser/uuid (core) + webpatser/laravel-uuid (Laravel integration)

Migration Steps:

  1. Update composer: composer require webpatser/laravel-uuid:^6.0
  2. Code remains identical - same API, same performance
  3. New features: High-performance Str macros and HasUuids replacement

From v4.x to v6.0 (Recommended)

// Old Laravel methods (still work, but slower)
$uuid = Str::uuid();                    // Uses Ramsey UUID
$ordered = Str::orderedUuid();          // Uses Ramsey UUID
use Illuminate\Database\Eloquent\Concerns\HasUuids; // Ramsey-based

// New high-performance methods (15-25% faster) 
$uuid = Str::fastUuid();                // 15% faster
$ordered = Str::fastOrderedUuid();      // 25% faster  
use Webpatser\LaravelUuid\HasUuids;     // Our optimized version

// Direct UUID generation (fastest)
use Webpatser\Uuid\Uuid;
$uuid = Uuid::v4();                     // Direct library access
$ordered = Uuid::v7();                  // Database-optimized UUIDs

Benefits:

  • βœ… 15-25% performance improvement
  • βœ… All UUID versions (1, 3, 4, 5, 6, 7, 8)
  • βœ… Modern PHP 8.2+ optimizations
  • βœ… Clean architecture with separated concerns
  • βœ… Backward compatible - existing code still works

πŸ§ͺ Testing Your Installation

Quick Test (Laravel Tinker):

php artisan tinker
// Test high-performance Str macros
use Illuminate\Support\Str;
$uuid = Str::fastUuid();                    // 15% faster than Str::uuid()
$ordered = Str::fastOrderedUuid();          // V7 database-optimized
echo "Generated: {$uuid}\n";

// Test direct UUID library access  
use Webpatser\Uuid\Uuid;
$uuid4 = Uuid::v4();                        // Random UUID
$uuid7 = Uuid::v7();                        // Timestamp ordered
echo "UUID4: {$uuid4}\nUUID7: {$uuid7}\n";

// Test binary storage (if using HasBinaryUuids)
$binary = Str::fastBinaryUuid();            // 16 bytes
$string = Str::binaryToUuid($binary);       // Convert back
echo "Binary: " . strlen($binary) . " bytes\n";
echo "String: {$string}\n";

Run Integration Tests:

php artisan test --filter="UuidIntegrationTest"

Expected: βœ… 10 tests passing, 67 assertions

πŸ§ͺ Core Library Documentation

For complete UUID documentation, including:

  • All UUID versions (1, 3, 4, 5, 6, 7, 8)
  • Performance benchmarks
  • RFC compliance details
  • Advanced usage

See the core library: webpatser/uuid

πŸ“„ License

MIT License. See LICENSE file.

🀝 Contributing

This package provides Laravel integration only. For UUID library improvements, contribute to webpatser/uuid.


πŸ“š Available Classes

This Laravel integration provides these classes:

  • Webpatser\LaravelUuid\UuidServiceProvider - Service provider (auto-discovered)
  • Webpatser\LaravelUuid\UuidFacade - Global Uuid facade
  • Webpatser\LaravelUuid\UuidCast - Eloquent cast for UUID columns
  • Webpatser\LaravelUuid\HasUuids - High-performance replacement for Laravel's trait
  • Webpatser\LaravelUuid\UuidMacros - Str macro replacements (auto-registered)

Core UUID functionality comes from: Webpatser\Uuid\Uuid


Laravel integration for the fastest PHP UUID library.