High-performance UUID integration for Laravel - Drop-in replacement components using the blazing fast webpatser/uuid library that's 15% faster than Ramsey UUID.
Previous versions (β€5.x): Standalone UUID implementation
Version 6.0+: Laravel integration layer using separate webpatser/uuid core library
-
ποΈ 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
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
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
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 |
composer require webpatser/laravel-uuid
Requirements:
- PHP 8.2+
- Laravel 11.x or 12.x
- ποΈ 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
v6.0.0 is thoroughly tested and production-ready:
- β
MariaDB/MySQL: Tested with
varbinary(16)
anduuid
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
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 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.");
}
},
],
];
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!
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();
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
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!
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
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
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);
}
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 | 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)
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
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:
- Update composer:
composer require webpatser/laravel-uuid:^6.0
- Code remains identical - same API, same performance
- New features: High-performance Str macros and HasUuids replacement
// 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
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
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
MIT License. See LICENSE file.
This package provides Laravel integration only. For UUID library improvements, contribute to webpatser/uuid.
This Laravel integration provides these classes:
-
Webpatser\LaravelUuid\UuidServiceProvider
- Service provider (auto-discovered) -
Webpatser\LaravelUuid\UuidFacade
- GlobalUuid
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.