rhumsaa/uuid
Deprecated PHP 5.3+ library for generating and working with RFC 4122 UUIDs (v1, v3, v4, v5). No longer maintained—use ramsey/uuid instead.
composer require ramsey/uuid
use Ramsey\Uuid\Uuid;
$uuid = Uuid::uuid4(); // Random UUID (v4)
$uuidString = $uuid->toString();
ramsey/uuid).ramsey/uuid's Doctrine\DBAL\Types\UuidType for database columns (if needed).Uuid::uuid1() (time-based),Uuid::uuid4() (random),Uuid::uuid5(Uuid::NAMESPACE_DNS, 'domain.com') (namespace-based).use Illuminate\Database\Eloquent\Model;
use Ramsey\Uuid\Uuid;
class Post extends Model {
protected $keyType = 'string';
public $incrementing = false;
protected $primaryKey = 'id';
protected static function boot() {
parent::boot();
static::creating(function ($model) {
$model->{$model->primaryKey} = Uuid::uuid4()->toString();
});
}
}
$keyType = 'string' and $incrementing = false to disable auto-increment.// PostgreSQL (recommended)
Schema::create('posts', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('title');
$table->timestamps();
});
// MySQL (requires UUID extension or `char(36)` fallback)
Schema::create('posts', function (Blueprint $table) {
$table->char('id', 36)->primary();
$table->string('title');
$table->timestamps();
});
return response()->json([
'data' => [
'id' => $post->id, // UUID string
'title' => $post->title,
]
]);
Uuid validator:
use Illuminate\Validation\Rule;
$request->validate([
'id' => ['required', 'uuid'],
]);
// Version 5 UUID for a DNS namespace (e.g., 'example.com')
$uuid = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'example.com');
use Ramsey\Uuid\Doctrine\UuidType;
// In a migration (if using Doctrine outside Laravel)
$table->addColumn('id', UuidType::UUID_TYPE);
Uuid::uuid4() for predictable test IDs (e.g., Uuid::fromString('550e8400-e29b-41d4-a716-446655440000')).rhumsaa/uuid with ramsey/uuid via:
composer require ramsey/uuid --update-with-dependencies
32-bit PHP Limitations:
getMostSignificantBits() may throw UnsatisfiedDependencyException on 32-bit systems.ramsey/uuid (which handles this better) or ensure 64-bit PHP.UUIDv1 Time Dependence:
Uuid::uuid4() for randomness or Uuid::uuid5() for deterministic IDs.Doctrine Integration:
Doctrine\UuidType is deprecated in favor of native database UUID types (PostgreSQL/MySQL 8.0+).Schema::uuid() in Laravel migrations instead.Performance:
Validation Quirks:
Uuid::isValid() may return false for UUIDs with uppercase letters (e.g., "550E8400-E29B-41D4-A716-446655440000").strtolower() before validation.Uuid::isValid($string) to validate manually.uuid-ossp extension; MySQL: UUID() function or char(36)).date('c').$customNamespace = Uuid::NAMESPACE_URL; // or define your own
$uuid = Uuid::uuid5($customNamespace, 'https://example.com/resource');
$binaryUuid = Uuid::uuid4()->getBytes(); // Raw binary format
// config/uuid.php
return [
'default_version' => 'uuid4',
];
// AppServiceProvider
Uuid::setFactory(new \Ramsey\Uuid\Factory\UuidFactory());
uuid-ossp extension for gen_random_uuid().char(36) if UUID support is lacking (store as string).TEXT (no native UUID type).rhumsaa/uuid to ramsey/uuid:
use Rhumsaa\Uuid\Uuid; with use Ramsey\Uuid\Uuid;.uuid1(), uuid4(), etc.) remain unchanged.uuid() (PostgreSQL) or char(36) (MySQL/SQLite).How can I help you explore Laravel packages today?