kduma/eloquent-tokenable

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Allows using tokens (HashIDs) instead of id in Laravel Eloquent models.

v1.1.2 2020-10-06 23:28 UTC

This package is auto-updated.

Last update: 2021-10-17 21:41:52 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License SensioLabsInsight StyleCI

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 your APP_KEY.

  • protected $length = 10;
    A salt length. Default is 10.

  • protected $alphabet = 'qwertyuiopasdfghjklzxcvbnm1234567890';
    A hash alphabet. Default is abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

Usage

  • $model->token - Generate tokens
  • Model::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