kduma / eloquent-tokenable
Allows using tokens (HashIDs) instead of id in Laravel Eloquent models.
Requires
- php: >=5.4.0
- hashids/hashids: ^1.0
- illuminate/database: 5.*|^6.0|^7.0|^8.0
This package is auto-updated.
Last update: 2024-10-29 18:55:12 UTC
README
Allows using tokens (HashIDs) instead of id in Laravel Eloquent models.
Setup
Add the package to the require section of your composer.json and run composer update
"kduma/eloquent-tokenable": "^1.1"
Prepare models
In your model add following lines:
use \KDuma\Eloquent\Tokenable;
protected $appends = array('token');
Optionally you can add also:
-
protected $salt = 'SALT';
A salt for making hashes. Default is table name. This salt is added to yourAPP_KEY
. -
protected $length = 10;
A salt length. Default is 10. -
protected $alphabet = 'qwertyuiopasdfghjklzxcvbnm1234567890';
A hash alphabet. Default isabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
Usage
$model->token
- Generate tokensModel::whereToken($id)->first()
- Find by token. (whereToken
is query scope)
Hashids
A special thanks to creators of hashids, a PHP class that this package is based.
Packagist
View this package on Packagist.org: kduma/eloquent-tokenable