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.
Illuminate\Contracts\Container\BindingResolutionException compatibility). Ideal for:
UuidInterface/UlidInterface as value objects in entities (e.g., User::id() returns UuidV7).Symfony\Component\Serializer).UUIDv7 enables chronological event replay without manual timestamps.UUID, MySQL BINARY(16)), string storage (RFC 4122/9562), and BASE_58 for compactness. Avoids vendor lock-in.UidFactoryInterface to UuidV7Factory (default) or UlidFactory in AppServiceProvider.protected $incrementing = true with protected $keyType = 'string' and cast id to UuidInterface.Uuid::isValid() or Laravel’s UuidValidator (via symfony/validator).RFC_9562 (camelCase) or BASE_58 for compact payloads.Ramsey\Uuid\Uuid with Symfony\Uid\Uuid in new features.UuidInterface to existing models via trait or abstract class.ALTER TABLE users MODIFY id BINARY(16)).Str::uuid()) with UidFactory.| Risk Area | Mitigation Strategy | Impact Assessment |
|---|---|---|
| Backward Compatibility | Use abstract base classes (e.g., AbstractUuidModel) to wrap legacy IDs during migration. |
Low |
| Performance Overhead | Benchmark UuidV7 vs. Ulid generation (both <100ns on x64). Binary storage in DB reduces I/O by 20–30%. |
Negligible |
| Clock Skew (UUIDv7) | Leverage microsecond precision and entropy improvements (v7.3.1+). For multi-region, use NTP-synchronized clocks or ULIDs (monotonic). | Medium (addressable) |
| Testing Complexity | Use MockUuidFactory for deterministic UUIDs in CI/CD. |
Low |
| Database Schema Changes | Binary storage (PostgreSQL/MySQL 8.0+) is recommended for performance. Fallback to CHAR(36) if needed. |
Medium (one-time cost) |
| Team Adoption | Provide Laravel-specific examples (e.g., UuidModel, UlidModel traits) and migration scripts. |
Low (if documented well) |
BINARY(16))? If not, will CHAR(36) suffice?/share/2J...)?MockUuidFactory)?| Laravel Component | Integration Strategy | Example Implementation |
|---|---|---|
| Eloquent Models | Replace $incrementing = true with $keyType = 'string' and cast id to UuidInterface. Use UuidModel trait for boilerplate. |
```phpuse Symfony\Uid\Uuid;use Symfony\Uid\AbstractUuid;class User extends Model { protected $keyType = 'string'; protected $casts = [ 'id' => Uuid::class,<br> ];<br> public function getRouteKey(): string {<br> return $this->id->toRfc9562(); }} |
| API Responses | Serialize to RFC_9562 (camelCase) or BASE_58 for compactness. Use Symfony\Component\Serializer or Laravel’s JsonSerializable. |
```phpreturn response()->json([ 'id' => $user->id->toRfc9562(),<br> 'name' => $user->name,]); |
| Validation | Use Uuid::isValid() or Laravel’s UuidValidator (via symfony/validator). |
```phpuse Symfony\Component\Validator\Constraints as Assert;class CreateUserRequest { #[Assert\Uuid] public string $externalId;} |
| Database Schema | Prefer BINARY(16) for UUIDs/ULIDs (PostgreSQL/MySQL 8.0+). Fallback to CHAR(36) if needed. |
```sqlALTER TABLE users MODIFY id BINARY(16);-- or --ALTER TABLE users MODIFY id CHAR(36) CHARACTER SET ascii COLLATE ascii_bin; |
| Event Sourcing | Use UuidV7 for time-ordered events. Store as binary in DB for efficiency. |
```phpclass OrderCreated { public UuidV7 $orderId; public Carbon $occurredOn;} |
| Testing | Use MockUuidFactory for deterministic UUIDs in CI/CD. |
```php$factory = new MockUuidFactory('123e4567-e89b-12d3-a456-426614174000');$user = new User();$user->id = $factory->generate(); |
Ramsey\Uuid, custom logic).UuidModel/UlidModel traits for new models.Ramsey\Uuid with Symfony\Uid in new features.How can I help you explore Laravel packages today?