PHPackages                             tiemsky/haskey - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tiemsky/haskey

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tiemsky/haskey
==============

A Laravel package to auto-generate unique keys based on models instead using traditional id.

1.0.0(1y ago)02MITPHPPHP ^8.3

Since Nov 13Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Tiemsky/haskey)[ Packagist](https://packagist.org/packages/tiemsky/haskey)[ RSS](/packages/tiemsky-haskey/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

HasKey
======

[](#haskey)

The `HasKey` package provides a trait for Laravel Eloquent models to automatically generate unique keys based on the model's class name and a random string. The length of the class name substring and the random string length are configurable.

Features
--------

[](#features)

- Generates a unique key automatically when creating a new model.
- Customizable substring length and random key length via configuration.
- Simple to integrate with any Eloquent model.

Installation
------------

[](#installation)

### 1. Install the Package

[](#1-install-the-package)

In your Laravel project, add the package to your `composer.json` by requiring it:

```
composer require tiemksy/haskey
```

### 2. Publish the Configuration (Optional)

[](#2-publish-the-configuration-optional)

To customize the length of the class name substring and the random string in the key, publish the package's configuration file:

```
php artisan vendor:publish --tag=config --provider="Tiemsky\HasKey\HasKeyServiceProvider"
```

This command will create a configuration file at config/haskey.php. Configuration

After publishing the configuration file, you can adjust the following settings in config/haskey.php:

return \[ 'substring\_length' =&gt; 3, // Default length of the class name prefix (customizable) 'key\_length' =&gt; 10, // Default length for the random string part of the key (customizable) \];

Default Values
--------------

[](#default-values)

```
substring_length: Defines the length of the prefix derived from the model’s class name. Defaults to 3.
key_length: Sets the length of the random string appended to the key. Defaults to 10.

```

Usage
-----

[](#usage)

```
Add the HasKey Trait to Your Model

To use the HasKey trait, simply add it to any Eloquent model where you want a unique key to be generated.
