tailflow / laravel-human-readable-keys
Generate Stripe-like IDs for Eloquent models
1.0
2021-02-04 08:24 UTC
Requires
- php: >=7.2
- illuminate/contracts: >=5.7
- illuminate/database: >=5.7
- illuminate/support: >=5.7
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/testbench: ^3.7|^4.0|^5.0|^6.0
- phpunit/phpunit: ^6.0|^7.0|^8.0|^9.0
README
Have you ever wanted to generate Stripe-like IDs for your Eloquent models? This package does exactly that!
Installation
You can install the package via composer:
composer require tailflow/laravel-human-readable-keys
Usage
- Change type of the
id
(or whatever your primary key column is) tostring
in the migration
Schema::create('users', function (Blueprint $table) { $table->string('id'); ... });
- Add
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; ... }
- (Optional) Customize key prefix and length
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; } }
License
The MIT License (MIT). Please see License File for more information.