{"id":46506868,"url":"https://github.com/veelasky/laravel-hashid","last_synced_at":"2026-03-06T15:02:17.798Z","repository":{"id":29244581,"uuid":"118424643","full_name":"veelasky/laravel-hashid","owner":"veelasky","description":"HashId Implementation on Laravel Eloquent ORM","archived":false,"fork":false,"pushed_at":"2025-11-25T13:15:30.000Z","size":589,"stargazers_count":43,"open_issues_count":0,"forks_count":18,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-28T18:13:03.900Z","etag":null,"topics":["eloquent","hacktoberfest","hash","hashid","hashids","hashing","laravel","laravel-hashid"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/veelasky.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2018-01-22T07:55:50.000Z","updated_at":"2025-11-25T13:15:34.000Z","dependencies_parsed_at":"2023-09-27T11:57:40.243Z","dependency_job_id":"d0d66d3b-26c4-4885-a926-b82ba5d3d9b2","html_url":"https://github.com/veelasky/laravel-hashid","commit_stats":{"total_commits":97,"total_committers":13,"mean_commits":7.461538461538462,"dds":0.6701030927835052,"last_synced_commit":"8ef7e2c7f6dde028fcb8f73115a430fbb34d454b"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/veelasky/laravel-hashid","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/veelasky%2Flaravel-hashid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/veelasky%2Flaravel-hashid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/veelasky%2Flaravel-hashid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/veelasky%2Flaravel-hashid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/veelasky","download_url":"https://codeload.github.com/veelasky/laravel-hashid/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/veelasky%2Flaravel-hashid/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30182686,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-06T14:42:24.748Z","status":"ssl_error","status_checked_at":"2026-03-06T14:42:14.925Z","response_time":250,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","hacktoberfest","hash","hashid","hashids","hashing","laravel","laravel-hashid"],"created_at":"2026-03-06T15:02:16.912Z","updated_at":"2026-03-06T15:02:17.791Z","avatar_url":"https://github.com/veelasky.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel HashId\n\n![CI/CD Pipeline](https://github.com/veelasky/laravel-hashid/workflows/CI%2FCD%20Pipeline/badge.svg)\n![🔒 Security Scanning](https://github.com/veelasky/laravel-hashid/workflows/%F0%9F%94%92%20Security%20Scanning/badge.svg)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/3e929b5327a9453bb0da5cbf2ecb8794)](https://app.codacy.com/gh/veelasky/laravel-hashid?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=veelasky/laravel-hashid\u0026utm_campaign=Badge_Grade)\n[![codecov](https://codecov.io/gh/veelasky/laravel-hashid/branch/master/graph/badge.svg?token=t95ymsMyDX)](https://codecov.io/gh/veelasky/laravel-hashid)\n[![Latest Stable Version](https://poser.pugx.org/veelasky/laravel-hashid/v)](//packagist.org/packages/veelasky/laravel-hashid)\n[![StyleCI](https://github.styleci.io/repos/118424643/shield?branch=master)](https://github.styleci.io/repos/118424643?branch=master)\n[![Total Downloads](https://poser.pugx.org/veelasky/laravel-hashid/downloads)](//packagist.org/packages/veelasky/laravel-hashid)\n[![Dependents](https://poser.pugx.org/veelasky/laravel-hashid/dependents)](//packagist.org/packages/veelasky/laravel-hashid)\n[![License](https://poser.pugx.org/veelasky/laravel-hashid/license)](//packagist.org/packages/veelasky/laravel-hashid)\n\n\u003e Automatic HashId generator for your Laravel Eloquent models.\n\n## About\n\nLaravel HashId provides an elegant way to add hashed IDs to your Eloquent models. It generates unique, non-sequential hashes for your model IDs and provides convenient methods to work with them.\n\n## ✨ Latest Features\n\n**Version 3.2.3** introduces automatic route key generation, eliminating boilerplate code, along with enhanced secure route model binding and powerful column selection capabilities for full Laravel 11/12 and PHP 8.4 compatibility.\n\n### 🚀 Automatic Route Key Generation (New in v3.2.3)\n\n**Zero boilerplate:** The `HashableId` trait now automatically provides `getRouteKey()` method:\n\n```php\nclass User extends Model\n{\n    use HashableId;\n    // getRouteKey() automatically returns hash - no implementation needed!\n}\n\nroute('users.show', $user); // Automatically generates: /users/k1jTdv6l\n```\n\n### 🔒 Secure Route Model Binding (New in v3.2.2)\n\n**Security improvement:** Route model binding now only accepts valid hash values by default, preventing predictable ID enumeration attacks:\n\n```php\nclass User extends Model\n{\n    use HashableId;\n    // Default: only hash resolution, numeric IDs return 404\n}\n\n// ✅ Secure: /users/k1jTdv6l works\n// ❌ Blocked: /users/1 returns 404 (prevents ID enumeration)\n```\n\n### 🔥 Column Selection API (New in v3.2.0)\n\n```php\n// Get user by hash with specific columns (better performance!)\n$user = User::byHash($hash, ['name', 'email']);\n\n// Get user by hash with single column\n$user = User::byHash($hash, ['name']);\n\n// Column selection with exception handling\n$user = User::byHashOrFail($hash, ['name', 'email']);\n```\n\n**Benefits:**\n- 🚀 **Better Performance** - Load only the columns you need\n- 🔒 **Type Safety** - Automatic primary key inclusion when required\n- 🔄 **Backward Compatible** - All existing code works unchanged\n- 🎯 **Smart Defaults** - `['*']` loads all columns, just like before\n- 🛡️ **Enhanced Security** - Prevents ID enumeration attacks by default\n- ✅ **Zero Boilerplate** - No manual `getRouteKey()` implementation needed\n\n## Compatibility\n\n### Modern Laravel Support (Recommended)\n\n| Laravel HashId | PHP Version | Laravel 10 | Laravel 11 | Laravel 12 |\n|----------------|-------------|-------------|-------------|-------------|\n| **3.2** 🌟      | **≥ 8.1**   | ✅          | ✅          | ✅          |\n| **4.x** 🚀      | **≥ 8.1**   | ✅          | ✅          | ✅          |\n\n- 🌟 **Stable Release (3.2)** - Recommended for production\n- 🚀 **Development Branch (4.x)** - Latest improvements\n\n### Full Version Matrix\n\n| Laravel HashId | PHP Version | Laravel 6 | Laravel 7 | Laravel 8 | Laravel 9 | Laravel 10 | Laravel 11 | Laravel 12 |\n|----------------|-------------|-----------|-----------|-----------|-----------|-------------|-------------|-------------|\n| **1.x**         | `≥ 7.0`   | ✅         | ❌         | ❌         | ❌         | ❌         | ❌         | ❌         |\n| **2.x**         | `≥ 7.2`   | ❌         | ✅         | ✅         | ✅         | ❌         | ❌         | ❌         |\n| **3.0**         | `≥ 7.4`   | ❌         | ✅         | ✅         | ✅         | ❌         | ❌         | ❌         |\n| **3.1**         | `≥ 8.0`   | ❌         | ✅         | ✅         | ✅         | ✅         | ❌         | ❌         |\n| **3.2** 🌟      | `≥ 8.1`   | ❌         | ❌         | ❌         | ❌         | ✅         | ✅         | ✅         |\n| **4.x** 🚀      | `≥ 8.1`   | ❌         | ❌         | ❌         | ❌         | ✅         | ✅         | ✅         |\n\n**📊 Version Recommendations:**\n- **Laravel 6-9** → Use `3.0` or `3.1`\n- **Laravel 10+** → Use `3.2` (stable) or `4.x` (development)\n- **Latest features** → Use `3.2+` with column selection support\n\n## Installation\n\n```bash\ncomposer require veelasky/laravel-hashid\n```\n\nWith Laravel's package auto-discovery, the package will be automatically registered.\n\n## Quick Start\n\nSimply add the `HashableId` trait to any Eloquent model you want to use with HashId:\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Veelasky\\LaravelHashId\\Eloquent\\HashableId;\n\nclass User extends Model\n{\n    use HashableId;\n}\n```\n\n## Usage Examples\n\n### Basic Usage\n\n```php\n$user = User::find(1);           // Find user by ID\n$user-\u003ehash;                     // Get HashId automatically\n\n// Find by HashId\n$user = User::byHash($hash);\n$user = User::byHashOrFail($hash); // Throws exception if not found\n\n// Convert between ID and HashId\n$hashedId = User::idToHash($id);\n$originalId = User::hashToId($hash);\n\n// Query scope\nUser::query()-\u003ebyHash($hash)-\u003eget();\n```\n\n### Column Selection (New in v3.2)\n\n```php\n// Load only specific columns for better performance\n$user = User::byHash($hash, ['name', 'email']);\n\n// Single column selection\n$user = User::byHash($hash, ['name']);\n\n// Column selection with exception handling\n$user = User::byHashOrFail($hash, ['name', 'email']);\n```\n\n### Persisting HashId to Database\n\n```php\nclass User extends Model\n{\n    use HashableId;\n\n    protected $shouldHashPersist = true;  // Persist hash to database\n    protected $hashColumnName = 'hashid';  // Custom column name (optional)\n}\n```\n\n### Route Model Binding\n\nThe trait automatically overwrites route methods to use HashId:\n\n```php\nRoute::get('/users/{user}', [UserController::class, 'show']);\n\nclass UserController\n{\n    public function show(User $user)\n    {\n        // $user resolves automatically by HashId\n        // Example URL: /users/k1jTdv6l\n        // Numeric IDs like /users/1 will return 404 by default (secure!)\n    }\n}\n```\n\n#### 🔒 Secure Route Model Binding (Default)\n\nBy default, route model binding only accepts valid hash values and returns 404 for plain numeric IDs, preventing predictable ID enumeration attacks:\n\n```php\nclass User extends Model\n{\n    use HashableId;\n    // $bindingFallback = false; // Default behavior - only hash resolution\n}\n\n// ✅ This works: /users/k1jTdv6l\n// ❌ This returns 404: /users/1 (prevents ID enumeration)\n```\n\n#### Optional Fallback to Numeric ID\n\nIf you need to support both hash and numeric ID resolution (not recommended for production), you can enable the fallback:\n\n```php\nclass User extends Model\n{\n    use HashableId;\n\n    protected $bindingFallback = true; // Allow both hash and numeric ID resolution\n}\n\n// ✅ Both work: /users/k1jTdv6l AND /users/1\n```\n\n#### Custom Field Binding\n\nCustom field binding always uses Laravel's default behavior:\n\n```php\nRoute::get('/users/{user:slug}', [UserController::class, 'show']);\n\n// This will resolve by 'slug' field, not by hash\n```\n\n#### 🚀 Automatic Route Key Generation (New in v3.2.3)\n\nThe `HashableId` trait now automatically provides `getRouteKey()` method, so you don't need to implement it manually:\n\n```php\nclass User extends Model\n{\n    use HashableId;\n    // No manual getRouteKey() implementation needed!\n}\n\n// Route generation now automatically uses hash\nroute('users.show', $user); // Generates: /users/k1jTdv6l\n```\n\n**Before this version:**\n```php\nclass User extends Model\n{\n    use HashableId;\n\n    // Manual implementation was required\n    public function getRouteKey() {\n        return $this-\u003ehash;\n    }\n}\n```\n\n**Benefits:**\n- ✅ **Zero Boilerplate** - No manual `getRouteKey()` implementation needed\n- 🔄 **Automatic** - Works out of the box with the trait\n- 🛡️ **Consistent** - Uses the same hash values as route resolution\n- 🔧 **Configurable** - Respects custom hash column names\n- ⬅️ **Backward Compatible** - Existing custom implementations still work\n\n### Validation Rules\n\n```php\nuse App\\Models\\User;\nuse Veelasky\\LaravelHashId\\Rules\\ExistsByHash;\n\n$request-\u003evalidate([\n    'user_id' =\u003e ['required', new ExistsByHash(User::class)],\n]);\n```\n\n### Advanced Usage\n\n#### Repository Pattern Access\n\n```php\n// Using the HashId facade\n$hashedId = HashId::idToHash($id, User::class);\n$originalId = HashId::hashToId($hash, User::class);\n\n// Manual hash ID creation\n$hashId = HashId::make('custom-key', 'custom-salt');\n```\n\n#### Shared Hash Across Models\n\n```php\nclass User extends Model\n{\n    protected $hashKey = 'shared-hash-key';\n}\n\nclass Customer extends User { }\n\n$customer = Customer::find(1);\n$user = User::find(1);\n\n// Both will have the same hash\necho $customer-\u003ehash === $user-\u003ehash; // true\n```\n\n## Configuration\n\nYou can configure HashId behavior using environment variables:\n\n```env\nHASHID_SALT=your-custom-salt\nHASHID_LENGTH=10\nHASHID_ALPHABET=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\n```\n\nOr publish the configuration file:\n\n```bash\nphp artisan vendor:publish --tag=laravel-hashid-config\n```\n\n## License\n\nMIT License. Feel free to use this package in your projects!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fveelasky%2Flaravel-hashid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fveelasky%2Flaravel-hashid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fveelasky%2Flaravel-hashid/lists"}