- How do I install and configure this package in Laravel?
- Run `composer require cirlmcesc/laravel-hashids` to install, then execute `php artisan hashids:install` to publish the config file. Customize the `config/hashids.php` settings like salt length, alphabet, and encoding rules.
- Does this package work with Laravel 10 or 11?
- The package supports Laravel 10+ and 11+ as of its latest release. Verify compatibility by checking the `laravel/framework` version constraints in the package’s `composer.json` or GitHub releases.
- Can I encode only specific fields (not just `_id` fields) in my model?
- Yes, use the `$_only_need_encode_fields` property to specify exact fields to encode. Avoid mixing it with `$_doesnt_need_encode_fields`—they’re mutually exclusive. For example, set `$_only_need_encode_fields = ['id', 'user_id']` to encode only those fields.
- Will this break existing route model binding in Laravel?
- No, the package automatically decodes Hashids in route parameters back to model IDs. For example, a route like `/product/{product}` will still bind the decoded ID to your controller method. Ensure your primary key is numeric for seamless integration.
- How do I manually encode/decode IDs outside of model serialization?
- Use the helper methods `Hashids::encode($id)` and `Hashids::decode($hashid)`. These are useful for custom logic, like generating shareable links or handling API responses where automatic encoding isn’t applicable.
- Is there a performance impact for high-traffic applications?
- Hashids encoding/decoding adds minimal overhead (~10–50µs per operation). For high-throughput apps, consider caching decoded IDs in Redis or shortening Hashids (e.g., 6 chars instead of 10) to reduce CPU load.
- Can I use this with UUID primary keys instead of auto-increment IDs?
- No, the package assumes numeric primary keys for Hashids encoding. For UUIDs, use Laravel’s built-in `Str::orderedUuid()` or a custom resolver to decode UUIDs in routes manually.
- How do I migrate existing models to use Hashids without downtime?
- The package doesn’t require schema changes, but you may need to backfill Hashids for existing records. Consider writing a migration script or using `php artisan tinker` to encode IDs in batches. Test thoroughly in staging first.
- What happens if two IDs generate the same Hashid (collision)?
- Collisions are statistically rare with a strong salt (default: 16 chars). Monitor decode failures in production and adjust the salt length or alphabet if needed. Log errors to detect collisions early.
- Are there alternatives to this package for Laravel?
- Yes, consider `spatie/laravel-hashid` (more actively maintained) or Laravel’s native `Str::orderedUuid()` for UUID-based obfuscation. Evaluate based on your needs—Hashids for short, readable IDs or UUIDs for uniqueness.