symfony/uid
Symfony UID component offers an object-oriented API to generate and work with unique identifiers. Includes ULIDs and UUIDs (v1 and v3–v8), with implementations compatible with both 32-bit and 64-bit systems for consistent, portable IDs.
composer require symfony/uid
use Symfony\Uid\Uuid;
use Symfony\Uid\UuidFactory;
$factory = UuidFactory::v7();
$uuid = $factory->create(); // e.g., "018d4f98-7f3b-7e4c-8d4f-987f3b7e4c8d"
use Symfony\Uid\Ulid;
$ulid = Ulid::generate(); // e.g., "01H5Z3X9X9X9X9X9X9X9X9X9X9"
Replace Laravel’s default auto-increment IDs with UUIDs in a model:
use Illuminate\Database\Eloquent\Model;
use Symfony\Uid\Uuid;
use Symfony\Uid\UuidFactory;
class Post extends Model
{
protected $keyType = 'string';
public $incrementing = false;
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->{$model->getKeyName()} = UuidFactory::v7()->create();
});
}
}
| Class | Purpose |
|---|---|
UuidFactory |
Factory for generating UUIDs (v1, v3–v8). Defaults to v7. |
Ulid |
Generates ULIDs (Universally Unique Lexicographically Sortable IDs). |
AbstractUid |
Base class for all UIDs (UUIDs/ULIDs). Provides validation, formatting. |
MockUuidFactory |
For deterministic UUIDs in tests (e.g., MockUuidFactory::fromString()). |
Use for analytics, event sourcing, or time-window queries:
$factory = UuidFactory::v7();
$uuid = $factory->create(); // e.g., "018d4f98-7f3b-7e4c-8d4f-987f3b7e4c8d"
Use for privacy-sensitive data (e.g., user IDs):
$uuid = Uuid::v4(); // e.g., "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
Use for deterministic IDs from namespaces (e.g., URLs, DNS):
$factory = UuidFactory::v5('6ba7b810-9dad-11d1-80b4-00c04fd430c8'); // DNS namespace
$uuid = $factory->create('example.com'); // e.g., "d3b07384-435f-5396-8554-039c2ffa8f69"
$ulid = Ulid::generate(); // e.g., "01H5Z3X9X9X9X9X9X9X9X9X9X9"
// Schema migration
Schema::create('posts', function (Blueprint $table) {
$table->uuid('id')->primary(); // or ->binary(16) for UUIDs
$table->string('title');
$table->timestamps();
});
binary(16) for UUIDs to save space and improve performance:
$table->binary('id')->primary();
For MySQL <8.0 or Oracle, use char(36):
$table->char('id', 36)->primary();
use Symfony\Uid\Uuid;
// Validate a string
if (Uuid::isValid($string)) {
$uuid = Uuid::fromString($string);
}
// Serialize to different formats
$uuid = Uuid::v4();
echo $uuid->toRfc9562(); // "018d4f987f3b7e4c8d4f987f3b7e4c8d"
echo $uuid->toBase58(); // "2J..." (URL-safe)
echo $uuid->toBinary(); // Binary string for storage
use Symfony\Uid\MockUuidFactory;
$factory = MockUuidFactory::fromString('018d4f98-7f3b-7e4c-8d4f-987f3b7e4c8d');
$uuid = $factory->create(); // Always returns the same UUID
Register a global UUID factory in AppServiceProvider:
use Symfony\Uid\UuidFactory;
public function register()
{
$this->app->singleton('uuid.factory', function () {
return UuidFactory::v7();
});
}
Then inject it into controllers/services:
use Illuminate\Support\Facades\App;
class PostController extends Controller
{
public function store()
{
$uuid = App::make('uuid.factory')->create();
// ...
}
}
Use BASE_58 for compact, URL-safe IDs:
use Symfony\Uid\Uuid;
$uuid = Uuid::v4();
$shortUrl = route('post.show', ['id' => $uuid->toBase58()]);
/post/2J... (vs. /post/6ba7b810-9dad-11d1-80b4-00c04fd430c8).Use UUIDv7 for time-ordered events:
use Symfony\Uid\UuidFactory;
class OrderCreated implements ShouldBeStored
{
public function __construct(
public string $orderId,
public string $userId,
public array $items
) {}
public static function create(array $data): self
{
return new self(
orderId: UuidFactory::v7()->create(),
userId: $data['user_id'],
items: $data['items']
);
}
}
UUIDv1 Time Dependence
UUIDv7 (time-ordered but optimized for x64) or Ulid instead.Binary Storage in MySQL <8.0
BINARY(16) for UUIDs. Use CHAR(36) instead.CHAR(36) with a custom accessor:
protected $casts = [
'id' => 'string',
];
ULID vs. UUID Sorting
Ulid if you need shorter strings (26 chars) or UUIDv7 if you need RFC compliance.MockUuidFactory in Production
MockUuidFactory is for testing only. Never use it in production.UuidFactory::v7() or Ulid::generate() in production.Case Sensitivity in UUIDs
UUIDv1 defaults to lowercase in Symfony/UID.$uuid->toRfc
How can I help you explore Laravel packages today?