tailflow/laravel-human-readable-keys

Generate Stripe-like IDs for Eloquent models
1,919 7
Install
composer require tailflow/laravel-human-readable-keys
Latest Version:1.0
PHP:>=7.2
License:MIT
Last Updated:Dec 19, 2022
Links: GitHub  ·  Packagist
Maintainer: alexzarbn

Laravel Human Readable Keys

Latest Version on Packagist Build Status on Github Actions

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

  1. Change type of the id (or whatever your primary key column is) to string in the migration
Schema::create('users', function (Blueprint $table) {
    $table->string('id');
    
    ...
});
  1. 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;

   ...
}
  1. (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.

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

637,827,751 9,703
laravel/framework

The Laravel Framework.

584,532,650 34,931
laravel/tinker

Powerful REPL for the Laravel framework.

498,216,183 7,442
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

413,412,504 609
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

391,429,217 4,660