- How do I install nanoid-php in a Laravel project?
- Run `composer require hidehalo/nanoid-php` in your project root. The package has no Laravel-specific dependencies, so it integrates seamlessly with existing code. For Laravel 8+, autoloading is automatic.
- Can I use Nanoid for Eloquent model primary keys instead of UUIDs?
- Yes, Nanoid works perfectly for Eloquent primary keys. Replace `Str::uuid()` with `Nanoid::generate()` in your model’s `boot()` method or use a model observer. Ensure your database column is set to a string type (e.g., `VARCHAR(21)`).
- What’s the default length for Nanoid, and should I change it?
- The default length is 21 characters, offering ~2^134 possible combinations with negligible collision risk. For shorter IDs (e.g., 10 chars), reduce length but validate collision probability for your use case. Longer IDs (e.g., 25 chars) add security but increase storage/URL size.
- How does Nanoid handle collisions in high-traffic Laravel APIs?
- Collisions are astronomically unlikely with default settings, but you can implement retry logic if needed. For example, wrap generation in a loop that checks for duplicates in your database. Nanoid’s cryptographic randomness minimizes this risk even at scale.
- Is nanoid-php compatible with Laravel 10 and older versions like 8 or 9?
- Yes, the package is framework-agnostic and works with any PHP 8.0+ Laravel version (8.x, 9.x, 10.x). No Laravel-specific features are required, so it integrates without version conflicts. Test thoroughly if using custom configurations.
- Can I customize the alphabet used by Nanoid (e.g., exclude ambiguous characters)?
- Absolutely. Pass a custom alphabet to `Nanoid::generate($length, $alphabet)`. For example, exclude `l1O0` for clarity: `$nanoid = Nanoid::generate(10, 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789');`. This is useful for URLs or human-readable IDs.
- How does Nanoid perform compared to Laravel’s Str::uuid() or ramsey/uuid?
- Nanoid is slower than UUID generation (~2–5x) due to its customizable alphabet and cryptographic safety checks. For most Laravel apps, this is negligible unless generating >10K IDs/sec. Benchmark in your staging environment if performance is critical.
- Will Nanoid work with Redis or other non-relational databases as primary keys?
- Yes, Nanoid is database-agnostic. For Redis, ensure atomicity by generating IDs outside transactions or using Lua scripts. Nanoid’s URL-friendly format also works well for cache keys or distributed locks, avoiding special characters.
- How can I mock Nanoid in PHPUnit tests for unit testing?
- Use PHPUnit’s partial mocking to stub `Nanoid::generate()`. For example: `$mock = $this->getMockBuilder(Nanoid::class)->disableOriginalConstructor()->getMock(); $mock->method('generate')->willReturn('TEST123');`. This ensures deterministic test IDs without side effects.
- Are there any security risks I should know about when using Nanoid in production?
- Nanoid is cryptographically secure by default (uses `random_bytes()`), but ensure your custom alphabet doesn’t weaken entropy. Avoid predictable patterns (e.g., timestamps) and validate ID generation in staging. For sensitive tokens, combine with HMAC or short-lived signatures.