- How do I use Symfony UID with Laravel Eloquent models for UUID/ULID primary keys?
- Symfony UID integrates natively with Eloquent via casting. Define `$casts = ['id' => Uuid::class]` in your model, and Laravel will automatically handle serialization/deserialization. For ULIDs, use `$casts = ['id' => Ulid::class]`. Binary storage (e.g., `BINARY(16)`) in PostgreSQL/MySQL 8.0+ is recommended for performance.
- What’s the difference between UuidV4 and UuidV7 in Symfony UID, and which should I use in Laravel?
- UuidV4 generates random IDs, while UuidV7 combines timestamp + randomness for time-ordered sorting (like ULIDs). Use UuidV7 for event sourcing or time-based queries (e.g., `WHERE id > '2023-01-01'`), and UuidV4 for general-purpose randomness. Both work seamlessly with Eloquent and Laravel’s validator.
- Can I migrate from ramsey/uuid or Laravel’s Str::uuid() to Symfony UID without breaking changes?
- Yes. Symfony UID is backward-compatible with existing UUID strings (e.g., `123e4567-e89b-12d3-a456-426614174000`). Replace `Str::uuid()` with `Uuid::v4()` or `Uuid::v7()`, and update database schemas to use `BINARY(16)` for binary storage. Laravel’s Schema Builder supports `uuid()` and `ulid()` methods in migrations (v9.0+).
- How does Symfony UID handle ULID sorting in MySQL, which lacks native BINARY collation?
- For ULID sorting in MySQL, store ULIDs as `CHAR(26)` with `COLLATE utf8mb4_bin` and create a generated column with a custom index function. Alternatively, use `BINARY(16)` in PostgreSQL or MySQL 8.0+, which supports native binary sorting. Symfony UID’s `Ulid` objects ensure consistent generation regardless of storage format.
- Is Symfony UID compatible with Laravel 10+ and PHP 8.1+? What about older versions?
- Symfony UID requires PHP ≥8.1 (or ≥8.4 for newer features) and works flawlessly with Laravel 10+. For PHP 7.4–8.0, use Symfony UID v6.x. Laravel 9+ supports binary storage via `Schema::table()->uuid()` or `ulid()`, while older versions may need manual schema adjustments. Always check the [Symfony UID docs](https://symfony.com/doc/current/components/uid.html) for version-specific notes.
- How can I mock UUID/ULID generation in Laravel tests to avoid flaky time-based tests?
- Use Symfony UID’s `MockUuidFactory` or `MockUlidFactory` to generate deterministic IDs in tests. For example, inject a mock factory into your service container or use Laravel’s `partialMock` to override `Uuid::v7()`. This is especially useful for event sourcing or time-sensitive workflows where clock skew could cause test failures.
- What’s the performance impact of using BINARY(16) for UUIDs vs. CHAR(36) in Laravel?
- Binary storage (`BINARY(16)`) adds ~5% overhead compared to `BIGINT` but enables efficient sorting (critical for ULIDs/UuidV7) and reduces storage size by ~60% vs. `CHAR(36)`. In PostgreSQL, use the native `UUID` type for simplicity. For MySQL <8.0, `CHAR(36)` is the only option, but consider upgrading for binary support.
- Can Symfony UID work with Laravel’s API resources or Form Request validation?
- Absolutely. Use the `ValidUuid` rule in Laravel’s validator for request validation (e.g., `Rule::uuid()`). For API resources, implement `JsonSerializable` or `Arrayable` on your model to return UIDs as strings or binary data. Symfony UID’s `UidInterface` ensures consistent serialization across JSON, XML, and form submissions.
- How do I handle mixed UUID formats (e.g., legacy CHAR(36) vs. new BINARY(16)) in a Laravel migration?
- Use Laravel’s `Schema::table()` to add new columns with `BINARY(16)` while keeping legacy `CHAR(36)` columns. Add a migration step to populate the new column from the old one using `Uuid::fromString($legacyId)`. For queries, cast results to `Uuid` objects to unify handling. Tools like `doctrine/dbal` can help with complex format conversions.
- What are the alternatives to Symfony UID for UUID/ULID generation in Laravel, and why choose this one?
- Alternatives include `ramsey/uuid` (UUID-only), `ulid/ulid` (ULID-only), or Laravel’s built-in `Str::uuid()`. Symfony UID stands out by combining ULIDs and UUIDs (v1, v3–v8) in a single package with 32/64-bit compatibility, Eloquent integration, and Laravel-specific features like `ValidUuid` validation. It’s also part of Symfony’s ecosystem, ensuring long-term maintenance and consistency with other Symfony components.