cirlmcesc/laravel-hashids
Laravel package to obfuscate model IDs and route parameters using Hashids. Adds a model trait that automatically encodes ID and *_id fields on serialization, decodes for route model binding, and provides helper methods plus Artisan install/test commands.
abc123 instead of 123456). This aligns well with:
AUTO_INCREMENT) must be bypassed.hashids/hashids library (a PHP port of Hashids) ensures compatibility with PHP’s type system and Laravel’s dependency injection.boot() method (Laravel’s model booting mechanism).HashidScope to filter models by their Hashid values (e.g., Model::whereHashid('abc123')).Route::get('/product/{product}', ...)).hashid column (if storing generated IDs separately from the primary key).WHERE id = Hashids::decode('abc123')).| Risk Area | Description | Mitigation Strategy |
|---|---|---|
| Performance Overhead | Hashids generation/decoding adds CPU overhead (~10–50µs per operation, depending on salt length and ID length). For high-throughput systems (e.g., 10K+ requests/sec), this may introduce latency. | Benchmark under load; consider caching decoded IDs (e.g., Redis) or using shorter Hashids (e.g., 6 chars instead of 10). |
| Collision Risk | While statistically rare, Hashids collisions can occur if the salt is weak or IDs are excessively long. The package uses a random salt by default, but custom salts must be managed carefully. | Validate salt uniqueness across environments; monitor for collisions in production (log decode failures). |
| Route Binding Complexity | Implicit route binding (e.g., /product/{product}) requires the primary key to be numeric. If the primary key is a UUID or string, additional logic (e.g., a custom resolver) is needed. |
Document edge cases; provide fallback resolvers for non-numeric primary keys. |
| Migration Complexity | Retrofitting Hashids to existing models may require: 1. Adding a hashid column (if not using the primary key). 2. Backfilling existing records. 3. Updating queries, routes, and APIs. |
Offer a migration helper (e.g., php artisan hashids:migrate) to automate backfilling. Provide clear deprecation paths for sequential IDs. |
| Security Implications | Hashids are not cryptographically secure. They are reversible with the salt, so: - Avoid using them for sensitive data (e.g., passwords, tokens). - Ensure salts are stored securely. | Enforce salt management (e.g., environment variables); pair with other security measures (e.g., rate limiting on decode endpoints). |
| Versioning Risks | Last release is 2026-02-24 (future date as of writing). If the package is abandoned, forks or alternative solutions (e.g., Laravel’s built-in Str::orderedUuid()) may be needed. |
Evaluate fork activity; maintain a local fork with critical fixes. Monitor for Laravel version compatibility (e.g., 10.x, 11.x). |
Primary Key Strategy:
hashid column?ON DELETE CASCADE on numeric IDs)?Performance Requirements:
Security and Compliance:
Migration Path:
Monitoring and Observability:
hashid column) is indexed for performance.hashids/hashids (PHP port of Hashids).| Phase | Steps | Tools/Commands |
|---|---|---|
| Preparation | 1. Assess models requiring Hashids (prioritize user-facing entities). 2. Backup database. 3. Test Hashids generation in a staging environment. | composer require cirlmcesc/laravel-hashids |
| Configuration | 1. Publish the package config (php artisan vendor:publish --tag=hashids-config). 2. Set salt (e.g., APP_HASHIDS_SALT). 3. Configure id_length (default: 10). |
Config file: config/hashids.php |
| Model Integration | 1. Use the HasHashid trait or extend HashidModel (if provided). 2. Add hashid column to the database (if not using primary key). 3. Update model casts/accessors. |
Trait: use \Cirlmcesc\Hashids\Traits\HasHashid; |
| Backfilling | 1. Write a migration to generate Hashids for existing records. 2. Update foreign keys to reference Hashids if needed. | Custom migration or php artisan hashids:migrate (if provided) |
| Route/Query Updates | 1. Update routes to bind Hashids (e.g., Route::get('/product/{product}', ProductController::class)). 2. Update queries to support whereHashid(). |
Route model binding, Eloquent scopes |
| API/Client Updates | 1. Update API responses to include Hashids. 2. Deprecate sequential ID endpoints (add redirects or rate-limited fallbacks). | OpenAPI/Swagger docs, deprecation headers |
| Testing | 1. Test Hashid generation/decoding edge cases (e.g., max int, collisions). 2. Validate route binding and query scopes. 3. Load test under production-like conditions. | PHPUnit, Laravel Dusk, k6/locust for performance |
| Deployment | 1. Roll out in stages (e.g., non-c |
How can I help you explore Laravel packages today?