Package
laravel-key-factory
A simple package to generate Eloquent Model Keys
dev-main2,841 downloads6 stars
A simple package to generate Eloquent Model Keys
Laravel Key Factory is a simple package to generate Eloquent Model Keys for your models easily. This is something I do quite often, instead of using UUID’s which can be quite unfriendly to look at, I use Model keys which are unique strings generated for each model.
Installation
You can install the package via composer:
composer require juststeveking/laravel-key-factoryYou can publish and run the migrations with:
You can publish the config file with:
php artisan vendor:publish --provider="JustSteveKing\KeyFactory\KeyFactoryServiceProvider" --tag="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 = \JustSteveKing\KeyFactory\KeyFactory::generate( prefix: 'test', // 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: 'test', // what you want to prefix your keys with. length: 20, // optional - the default of 20 is set in the config.);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
An example:
- User Model:
use_12345678909876543212 - Project Model:
pro_12345678909876543212
Currently this isn’t a configured option as it is easy to override or implement yourself.
Testing
composer testChangelog
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.
Related
More Packages
mavis
Run a freelance business from plain files: clients, calls and follow-ups, time, quotes and invoices as Markdown, from one Go binary, with an MCP server for agents.
v0.1.3
PHPcloudevents
Cloud Events in PHP.
dev-main
PHPcompanies-house-laravel
A Laravel wrapper to get companies house information and validate company numbers
v1.2.0
Same subject
In other formats
Working with Laravel Model Events
Learn how to work with Laravel Model Events and explore different approaches to handle them efficiently. Discover pros and cons of each method.
PHP & Laravel
VideoLet's Build in Public with JustSteveKing: Creating a Unified Event Tracker
🚀 Welcome to "Let's Build in Public" with JustSteveKing! In this exciting livestream series, we're diving into the world of startup development, taking on the challenge of building a dynamic applicat
PHP & Laravel