Download the PHP package mcris112/laravel-hashidable without Composer
On this page you can find all versions of the php package mcris112/laravel-hashidable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mcris112/laravel-hashidable
More information about mcris112/laravel-hashidable
Files in mcris112/laravel-hashidable
Package laravel-hashidable
Short Description LaravelHashidable - Hashids for Laravel Models and Routes
License MIT
Informations about the package laravel-hashidable
Laravel Hashidable
Laravel Hashidable provides a seamless way to use Hashids in your Laravel models. It automatically handles encoding/decoding of IDs for routing and database lookups, keeping your internal IDs hidden from the public.
This package is an enhanced fork of the original kayandra/hashidable, featuring improved type safety, caching support, global helpers, and fluent query builder integration.
✨ Key Features
- 🛡️ Automatic Route Model Binding: Uses hashids in URLs instead of plain integers.
- 🚀 Performance Caching: Decoded hashids can be cached to improve performance.
- 🛠️ Fluent Scopes: Chainable methods like
whereHashid(),orWhereHashid(), andfindByHashid(). - 🧬 Relation Support: Easily load relations when finding by hashid using
with(). - ✅ Custom Validation: Built-in
hashid_existsrule for validating hashids in requests. - 🌍 Global Helpers: Simple
hashid_encode()andhashid_decode()functions. - 🎨 Customizable: Per-model configuration for salts, lengths, and alphabets.
📥 Installation
⚙️ Setup
Add the Hashidable trait to your Eloquent model:
🚀 Usage
Basic Operations
Advanced Querying & Relations
Thanks to the new fluent scopes, you can now load relationships while finding by hashid:
Validation Rule
You can validate that a hashid exists in the database using the hashid_exists rule. This automatically decodes the hashid before checking the database.
The rule accepts two parameters:
- The Model class or Table name.
- The Database column (optional, defaults to the model's primary key or
id).
Global Helpers
Decode or encode hashids anywhere without needing a model instance:
🔗 Route Model Binding
This package automatically handles Route Model Binding. Instead of IDs, your routes will use hashids:
Generating links automatically uses the hashid:
⚡ Performance Caching
If you find yourself decoding the same hashids frequently, you can enable caching in the config. This will store the decoded integer ID in your cache store.
🛠️ Configuration
Publish the configuration file:
Global Config (config/hashidable.php)
| Option | Description | Default |
|---|---|---|
salt |
Unique salt for hash generation | env(HASHIABLE_SALT) |
length |
Minimum length of generated hash | 16 |
charset |
Characters used in the hashid | a-zA-Z0-9 |
prefix |
Optional prefix for hashids | "" |
suffix |
Optional suffix for hashids | "" |
separator |
Separator between prefix/suffix | "-" |
Per-Model Configuration
Implement HashidableConfigInterface to customize settings for a specific model:
❓ FAQ
Q: Are hashes stored in the database?
A: No. Hashes are calculated dynamically based on your model's ID and salt.
Q: What happens if I change my salt?
A: All existing hashids will change. It's recommended to set a permanent salt in your .env file (HASHIABLE_SALT).
📄 License
The MIT License (MIT). Please see License File for more information.