amirkacem/model-key-factory

A simple package to generate Eloquent Model Keys

v1.0.1 2022-04-08 23:17 UTC

This package is auto-updated.

Last update: 2025-06-09 06:35:18 UTC


README

Latest Version on Packagist Stars

Installation

composer require amirkacem/model-key-factory

You can publish the config file with:

php artisan vendor:publish --provider="Eloquent\KeyFactory\KeyFactoryServiceProvider" --tag="model-key-factory-config"

This is the contents of the published config file:

return [
    'key' => [
        'length' => 20,
    ]
];

Usage

This is a very simple to use package. There are two available methods to generate the key:

Using the Key Factory

$key = \Eloquent\KeyFactory\KeyFactory::generate(
    prefix: 'tests', // what you want to prefix your keys with.
    length: 20, // optional - the default of 20 is set in the config.
);

Using the Str helper

$key = \Illuminate\Support\Str::key(
    prefix: 'tests', // what you want to prefix your keys with.
    length: 20, // optional - the default of 20 is set in the config.
);

Test

vendor/bin/phpunit

Eloquent Integration

There is an eloquent model trait available to use called HasKey which will:

When you are creating an eloquent model, the trait will be booted. It will get the first 3 characters of the Model name, force them to lowercase and append a "_" and use this as the prefix for the Key Factory