symfony/uid
Symfony UID provides an object-oriented API to generate and represent unique identifiers. Supports ULIDs and UUIDs (v1 and v3–v8) with implementations that work on both 32-bit and 64-bit CPUs.
Laravel Native Compatibility: Symfony/UID aligns perfectly with Laravel’s ecosystem, supporting:
Uuid casting for database storage/retrieval (e.g., protected $casts = ['id' => Uuid::class]).Request/Response objects via UuidInterface serialization (JSON, XML, form data).ValidUuid) and Form Requests.BINARY(16)) for UUIDs/ULIDs, optimizing PostgreSQL/MySQL 8.0+ performance.UuidV7 enables efficient event querying (e.g., WHERE id > '2023-01-01').Microservices Alignment: Standardizes ID generation across Laravel/Symfony microservices, reducing duplication (e.g., replacing ramsey/uuid + custom logic).
Multi-Region Resilience: UuidV7 (time + randomness) and Ulid mitigate clock-skew issues in distributed deployments.
Str::uuid() or Ramsey\Uuid with a single composer require symfony/uid and minimal config:
// config/app.php
'aliases' => [
'Uuid' => Symfony\Component\Uid\Uuid::class,
];
BIGINT/INT with BINARY(16) (PostgreSQL) or CHAR(36) (MySQL <8.0).BINARY(16) or CHAR(26) with collation BINARY for sorting.uuid() and ulid() methods (v9.0+).123e4567-e89b-12d3-a456-426614174000).| Risk Area | Mitigation Strategy |
|---|---|
| PHP Version Lock | Requires PHP ≥8.1 (v7.4.x) or ≥8.4 (v8.0+). Laravel 10+ supports both. |
| Database Migration | Use Laravel’s Schema::table() with uuid()/ulid() methods; test with phpunit-db. |
| Performance Impact | Binary storage (BINARY(16)) adds ~5% overhead vs. BIGINT but enables sorting. |
| Testing Complexity | MockUuidFactory reduces flakiness in time-sensitive tests (e.g., event sourcing). |
| Vendor Lock-in | MIT license; no proprietary APIs. Interoperable with ramsey/uuid/ulid packages. |
| ULID Sorting | Requires BINARY collation or custom index functions in MySQL. |
BINARY(16) for UUIDs/ULIDs (recommended for performance) or stick with CHAR(36) for compatibility?BIGINT IDs in existing tables? (e.g., composite keys, foreign references).UuidV7 (time-ordered) or UuidV4 (random) for new entities?UuidV1 for MAC-address-based IDs (e.g., IoT devices)?123e4567...) or binary in APIs? Impact on clients.BASE_58 for URL-safe identifiers (e.g., /share/2J...)?MockUuidFactory in CI/CD for deterministic test IDs?UuidGenerator facade for consistency across the codebase?Ramsey\Uuid or custom UUID logic in favor of Symfony/UID?CHAR(36) vs. new BINARY(16))?Uuid, Ulid) and Uuid accessors.ValidUuid rule and custom validators.JsonSerializable/Arrayable.MockUuidFactory for deterministic IDs in unit/feature tests.make:uuid and make:ulid commands (via Symfony Console).UUID type or BINARY storage.BINARY(16) for UUIDs/ULIDs; CHAR(36) fallback.BLOB or TEXT with custom collation.UidNormalizer) if using Symfony HTTP Client/Messenger.| Phase | Action Items | Tools/Libraries |
|---|---|---|
| Assessment | Audit existing UUID generation (e.g., Str::uuid(), ramsey/uuid). |
phpstan, phpmd |
| Dependency Setup | Add symfony/uid to composer.json; alias Uuid in config/app.php. |
Composer, Laravel Service Provider |
| Database Schema | Migrate tables to BINARY(16) for UUIDs/ULIDs; update migrations. |
Laravel Schema Builder, doctrine/dbal |
| Model Layer | Replace BigIncrements with Uuid casting; update fillable/guarded. |
Eloquent, laravel-shift/database |
| API Layer | Update DTOs/Resources to serialize UIDs as strings/binary; add BASE_58 for URLs. |
Laravel API Resources, spatie/array-to-xml |
| Testing | Replace fake()->uuid() with MockUuidFactory; update snapshot tests. |
PestPHP, PHPUnit |
| Deprecation | Phase out Ramsey\Uuid; replace custom UUID logic with Symfony/UID. |
deprecated attribute, laravel-deprecation |
123e4567-e89b-12d3-a456-426614174000) are valid inputs.\x12\x3e...) interoperate with databases like PostgreSQL.UuidV8, BASE_58) can be adopted incrementally.Uuid support in Schema Builder.Uuid class may conflict with custom App\Uuid. Use aliases:
'aliases' => [
'SymfonyUuid' => Symfony\Component\Uid\Uuid::class,
],
BINARY collation in MySQL.UuidV7 for time-ordered IDs in read-heavy workloads.CHAR(36) for legacy systems; add a Uuid::toString() fallback.BINARY(16) for UUIDs/ULIDs in performance-critical tables.BASE_58 for shareable links.symfony/uid package ensures compatibility.How can I help you explore Laravel packages today?