oneduo / laravel-human-keys
Provides configurable and customizable Stripe-like keys for your Eloquent models
Fund package maintenance!
oneduo
Requires
- php: ^8.0
- godruoyi/php-snowflake: ^2.1
- illuminate/contracts: ^9.0|^10.0
- spatie/laravel-package-tools: ^1.13.0
- tuupola/ksuid: ^2.1
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-11-08 11:33:39 UTC
README
A package to use human readable keys in your Laravel models. Inspired by Stripe's id generation procedures.
Enables you to have KSUID keys in your models, which are human readable and sortable.
Example:
pos_2JvL8Gv5mirjbIVAlSRFrC8EaWR
forModels/Post.php
usr_p6UEyCc8D8ecLijAI5zVwOTP3D0
forModels/User.php
Table of Contents
Installation
You can install the package via composer:
composer require oneduo/laravel-human-keys
You can publish the config file with:
php artisan vendor:publish --tag="human-keys-config"
This is the contents of the published config file:
return [ /* |-------------------------------------------------------------------------- | Generator |-------------------------------------------------------------------------- | | Used to define the generator to use for generating model keys. | | Supported: | - ksuid (abc_p6UEyCc8D8ecLijAI5zVwOTP3D0) | - snowflake (abc_1537200202186752) | | Default: ksuid | | Note: You may define your own generator by implementing the contract | Oneduo\LaravelHumanKeys\Contracts\Generator and passing | the class name to the generator config option. | | See the example below: | 'generator' => \App\Services\MyGenerator::class */ 'generator' => 'ksuid', ];
Usage
To get started, use the HasHumanKey
trait in your model:
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Oneduo\LaravelHumanKeys\Concerns\HasHumanKey; class Post extends Model { use HasHumanKey; }
When using the ksuid
generator, the generated key will something like this: pos_2JvL8Gv5mirjbIVAlSRFrC8EaWR
When using the snowflake
generator, the generated key will something like this: pos_451734027389370636
Overriding the key prefix
You may set your own key prefix for each model by implementing the following method:
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Oneduo\LaravelHumanKeys\Concerns\HasHumanKey; class Post extends Model { use HasHumanKey; public static function getKeyPrefix() : string { // prefix without _ underscore as it gets added by the generator return 'post_prefix' } }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.