pfrug / hash-id
Simple trait for Laravel to encode IDs in URLs, providing a secure and obfuscated way to expose model identifiers
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/pfrug/hash-id
Requires
- jenssegers/optimus: ^1.1
Requires (Dev)
- phpunit/phpunit: ^11.5
- squizlabs/php_codesniffer: ^3.11
This package is auto-updated.
Last update: 2025-10-19 18:05:57 UTC
README
A simple trait for Laravel to encode IDs in URLs, providing a secure and obfuscated way to expose model identifiers.
Installation
To install the package, run the following command:
composer require pfrug/hash-id
Configuration
To publish the configuration file, use:
php artisan vendor:publish --tag="hashid-config"
This will create the hashid.php configuration file in the config directory.
Usage
- To use the HashIdtrait, simply include it in your model:
use Pfrug\HashId; class Post extends Model { use HashId; }
- Getting the encoded ID in the route
In your route model binding, you can now use the encoded ID:
Route::get('/post/{post}', function (Post $post) { return $post; });
This will automatically encode the id of your model in URLs.
Regenerate Configuration Values
If you want to regenerate the encoding values for prime, inverse, and random, you can use the following command:
php artisan hashid:regenerate
This will update the hashid.php configuration file with new values.
Running Tests
To run the tests for the package, use the following command:
vendor/bin/phpunit --configuration "phpunit.xml" --filter=HashIdTest
License
MIT License