- How do I replace Laravel Eloquent auto-increment IDs with UUIDs (e.g., UUIDv7) in my models?
- Set `$keyType = 'string'` and `$incrementing = false` in your model, then override the `boot()` method to generate UUIDv7 on creation. Example: `static::creating(function ($model) { $model->{$model->getKeyName()} = Uuid::v7(); });`. This works with PostgreSQL, MySQL 8.0+, and SQL Server via `importFromSqlServer()`.
- Does **webpatser/uuid** support Laravel’s validation rules for UUIDs?
- Yes. Use the built-in `uuid` rule in Laravel’s `validate()` method, e.g., `'id' => ['required', 'uuid']`. For stricter control, extend the rule to reject deprecated versions (like UUIDv1) or enforce specific versions (e.g., UUIDv4 for tokens).
- What’s the performance impact of UUIDv7 vs. UUIDv4 in Laravel for high-traffic apps?
- UUIDv7 generation is ~500K/sec, sufficient for 99% Laravel use cases. UUIDv7’s embedded timestamp improves indexing in PostgreSQL/MySQL 8.0+ (e.g., `WHERE created_at > ...` via UUIDv7’s `time` property), while UUIDv4 remains faster for stateless operations like API tokens. Benchmark against `ramsey/uuid` for your specific workload.
- Can I use **webpatser/uuid** with SQL Server’s GUID type without manual conversion?
- Yes. Use `Uuid::importFromSqlServer($sqlGuid)` to convert SQL Server’s GUID binary format to a PHP UUID object, and `toSqlServerBinary()` to export back. This handles mixed-endianness automatically, but test rigorously if interoperability with legacy B2B systems is critical.
- How do I generate cryptographically secure UUIDs (e.g., for API keys or auth tokens) in Laravel?
- Use `Uuid::v4()` for random, cryptographically secure UUIDs. Example: `$token = Uuid::v4()->string;`. Store these tokens securely (e.g., encrypted in the database) or manage them via Laravel Sanctum/Passport. Avoid UUIDv1 for tokens due to MAC address exposure risks.
- What’s the best way to migrate an existing Laravel app from auto-increment IDs to UUIDs?
- Use a dual-write approach during migration: add a `uuid` column, populate it with `Uuid::v7()` for new records, and gradually phase out the old ID column. For large tables, use `ALTER TABLE` with `CONCURRENTLY` (PostgreSQL) or batch updates. Test with `CREATE INDEX CONCURRENTLY` to avoid locks.
- Does **webpatser/uuid** work with Laravel Scout for searchable UUID fields?
- Yes. Cast UUIDs to strings for Scout by using `Uuid::v7()->string` in your model. This ensures compatibility with Scout’s search indexing while preserving the UUID’s properties (e.g., `version`, `time`) for application logic.
- How can I validate that a UUID string is nil (all zeros) in Laravel?
- Use `Uuid::isNilUuid($uuidString)`. This checks for the RFC 4122 nil UUID (e.g., `00000000-0000-0000-0000-000000000000`). Example: `if (Uuid::isNilUuid($request->input('id'))) { ... }`. Nil UUIDs are useful for placeholder values in database migrations.
- What Laravel versions and PHP versions does **webpatser/uuid** support?
- **webpatser/uuid** requires PHP ^8.5 and is compatible with Laravel 10+. It has no external dependencies, so it integrates cleanly with any Laravel project meeting these requirements. For older PHP versions, consider alternatives like `ramsey/uuid` (PHP 7.2+).
- How do I generate name-based UUIDs (v3/v5) for consistent identifiers in Laravel?
- Use `Uuid::generate(3, 'namespace', Uuid::NS_DNS)` for MD5-based UUIDs or `Uuid::generate(5, 'namespace', Uuid::NS_DNS)` for SHA-1-based UUIDs. Example: `$uuid = Uuid::generate(5, 'user@example.com', Uuid::NS_DNS)->string;`. This ensures consistent IDs for the same input (e.g., user emails) across deployments.