Generate Stripe-like IDs for Eloquent models
tailflow/laravel-human-readable-keys is a Laravel package for generate stripe-like ids for eloquent models.
It currently has 7 GitHub stars and 1.866 downloads on Packagist (latest version 1.0).
Install it with composer require tailflow/laravel-human-readable-keys.
Discover more Laravel packages by tailflow
or browse all Laravel packages to compare alternatives.
Last updated
Have you ever wanted to generate Stripe-like IDs for your Eloquent models? This package does exactly that!
You can install the package via composer:
composer require tailflow/laravel-human-readable-keys
id (or whatever your primary key column is) to string in the migrationSchema::create('users', function (Blueprint $table) {
$table->string('id');
...
});
HasHumanReadableKey trait to a model<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Tailflow\HumanReadableKeys\Concerns\HasHumanReadableKey;
class User extends Model
{
use HasHumanReadableKey;
...
}
By default, a singular form of the table name is used as prefix for generated keys. You can customize that by overriding
the getKeyPrefix method on the model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Tailflow\HumanReadableKeys\Concerns\HasHumanReadableKey;
class User extends Model
{
use HasHumanReadableKey;
...
public function getKeyPrefix(): string
{
return 'account';
}
}
Generated keys contain a unique hash that is 24 characters in length. To customize hash length, override the getKeyLength method:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Tailflow\HumanReadableKeys\Concerns\HasHumanReadableKey;
class User extends Model
{
use HasHumanReadableKey;
...
public function getKeyLength(): string
{
return 16;
}
}
The MIT License (MIT). Please see License File for more information.