Download the PHP package deligoez/laravel-model-hashid without Composer
On this page you can find all versions of the php package deligoez/laravel-model-hashid. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download deligoez/laravel-model-hashid
More information about deligoez/laravel-model-hashid
Files in deligoez/laravel-model-hashid
Package laravel-model-hashid
Short Description Generate, Save, and Route Stripe/Youtube-like Hash IDs for Laravel Eloquent Models
License MIT
Homepage https://github.com/deligoez/laravel-model-hashid
Informations about the package laravel-model-hashid
Generate, save, and route Stripe-like Hash Ids for your Laravel Eloquent Models.
Hash Ids are short, unique, and non-sequential identifiers that hide database row numbers from users. For more information, visit hashids.org.
For a User model with an id of 1234, you can generate Hash Ids like user_kqYZeLgo.
You have complete control over Hash Id length, prefix, separator, and alphabet. Check out the configuration section for details.
Table of Contents
- Requirements
- Installation
- Usage
- Hash Id Generation
- Query Builder Functions
- Route Model Binding (Optional)
- Saving Hash Ids to Database (Optional)
- Generic Hash Ids
- Blueprint Macro
- Hash Id Cast
- Serialization
- Blade Directive
- Decrypting Hash Ids in Form Requests
- Artisan Commands
- Validation
- Hash Id Terminology
- Configuration
- Upgrading
- Testing
- Changelog
- Contributing
- Security Vulnerabilities
- Credits
- License
Requirements
| Package | PHP | Laravel |
|---|---|---|
| ^4.0 | ^8.3 | ^11.0, ^12.0, ^13.0 |
| ^3.0 | ^8.2 | ^9.0 - ^11.0 |
| ^2.0 | ^8.1 | ^9.0 - ^12.0 |
| ^1.0 | ^8.0 | ^8.0 |
Installation
-
Install via Composer:
- Publish the config file:
Usage
Hash Id Generation
Add the HasHashId trait to any Eloquent model:
This gives you hashId and hashIdRaw attributes, plus a keyFromHashId() static method:
Query Builder Functions
All finding-related query builder functions work with Hash Ids:
Route Model Binding (Optional)
Add the HasHashIdRouting trait to enable route model binding with Hash Ids:
Implicit Binding
Explicit Binding
Register a custom model key in your RouteServiceProvider:
Saving Hash Ids to Database (Optional)
Add the SavesHashId trait to automatically persist Hash Ids:
Set the database_column in your configuration file (default: hash_id). You can configure it globally or per model.
Hash Id generation works on the fly -- saving to the database is not required for generation or decoding.
Since Hash Id generation requires an integer model key, saving to the database results in an additional query after model creation.
Generic Hash Ids
Use the HashId utility class to encode and decode integers without a model:
All parameters are optional and fall back to config values.
Blueprint Macro
A hashId() macro is available on the Blueprint class for migrations:
The default column name comes from the database_column config value.
Hash Id Cast
Use HashIdCast to cast attributes in your model:
get(): returns the stored string as-isset(): converts integers to a full Hash Id, passes strings through, and handles null
Serialization
Add the SerializesHashId trait to replace the primary key with the Hash Id in serialized output (arrays and JSON):
This follows the Stripe pattern of exposing Hash Ids in API responses. It respects $hidden and $visible attributes.
Blade Directive
Use the @hashid directive to output a model's Hash Id in Blade templates:
The output is XSS-safe via Laravel's e() helper.
Decrypting Hash Ids in Form Requests
Add the DecryptsHashIds trait to a FormRequest to automatically convert Hash Id inputs to integer keys after validation:
Artisan Commands
Two Artisan commands are available for encoding and decoding Hash Ids:
Validation
Two validation rules are provided for validating Hash Ids in form requests and validators.
ValidHashId — Format Validation (No Database Hit)
Checks if a value is a valid Hash Id format. Optionally validates against a specific model (prefix + decode check).
HashIdExists — Database Existence Check
Checks if a Hash Id corresponds to an existing database record. Equivalent to Laravel's exists rule for Hash Ids.
Both rules throw an InvalidArgumentException if the given model does not use the HasHashId trait.
Hash Id Terminology
A Hash Id consists of three parts:
| Part | Example | Required |
|---|---|---|
| Prefix | user |
No |
| Separator | _ |
No |
| Raw Hash Id | kqYZeLgo |
Yes |
You can generate Hash Ids with or without a prefix. Set prefix_length to 0 in the config to disable prefixes.
Configuration
Publish the config file and customize:
| Option | Default | Description |
|---|---|---|
salt |
'your-secret-salt-string' |
Salt for Hash Id generation. Change this before deploying. |
length |
13 |
Raw Hash Id length (excluding prefix and separator) |
alphabet |
'abcdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ234567890' |
Characters used in Hash Id generation (min 16 unique chars) |
prefix_length |
3 |
Prefix length from class name. -1 for full name, 0 for none |
prefix_case |
'lower' |
Prefix case: lower, upper, camel, snake, kebab, title, studly, plural_studly |
separator |
'_' |
Separator between prefix and raw Hash Id |
database_column |
'hash_id' |
Database column name for SavesHashId trait |
Model-Specific Configuration
Override any option per model in the model_generators array:
Upgrading
If you are upgrading from v3 to v4, please see the UPGRADE guide for a list of breaking changes.
Testing
This runs the full quality gate: Rector, Pint, PHPStan, and Pest.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Yunus Emre Deligoz
- Scott Plunkett
- Wade
- Laravel Shift
- Faruk
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-model-hashid with dependencies
ext-mbstring Version *
hashids/hashids Version ^4.1|^5.0
illuminate/contracts Version ^11.0|^12.0|^13.0
