hexify/laravel-id-customizer
Laravel package for generating custom IDs for models or any table column. Create prefixed incremental IDs (with optional reset on prefix change) or random IDs with configurable length, character set, and extras. Use via a controller helper or a model trait.
A laravel package to customize IDs formats.
In many laravel applications, using standar behaviors for generating IDs is not good idea for some Models. That's why many devs like to generate application IDs as prefixed auto incremental and someone like to generate their unique ID in a custom format.
This package will help you to generate a custom primary key or any field in your table with a custom ID.
composer require hexify/laravel-id-customizer
You can use it in your controller or inside your model by using trait.
First import ID Customizer in controller use Hexify\LaraIdCustomizer\IdCustomizer;.
Then in your method :
public function store(Request $request){
$config = [
'model' => Student::class,
'column' => 'uid',
'length' => 10,
'prefix' => date('ym')
];
$uid = IdCustomizer::generate($config);
$student = new Student();
$student->uid = $uid; // a fillable field.
$student->name = $request->input('name');
...
$student->save();
}
In your model :
-Implement the interface Hexify\LaraIdCustomizer\IdFactory;
-Add the trait Hexify\LaraIdCustomizer\Traits\HasIdFactory;.
...
use Hexify\LaraIdCustomizer\IdFactory;
use Hexify\LaraIdCustomizer\Traits\HasIdFactory;
class Student extends Model implements IdFactory {
...
use HasIdFactory;
...
}
Add this array below in your model to customize the uid field value.
/**
* The configs that used for generating custom ID.
*
* @var array
*/
private static $idFactoryConfig =
\\******************* INCREMENTAL *******************
[
'factory_method' => self::INCREMENTAL, \\ Optional. Default is self::INCREMENTAL.
'column' => 'uid', \\ Default is 'id'.
'length' => 10, \\ Default is 10.
'prefix' => 'STD-', \\ Or for example 'USR-'. Default is ''.
'reset_on_prefix_change' => false, \\ Default is true.
];
\\********************* RANDOM **********************
[
'factory_method' => self::RANDOM,
'column' => 'uid',
'length' => 10,
'prefix' => '',
'set' => self::NUMERIC, \\ Or self::ALPHA | self::ALPHA_NUMERIC. Default is self::ALPHA_NUMERIC.
'extra' => 'abcdef', \\ Default is ''.
];
Or private static $idFactoryConfig = []; for default configuration.
There is two methods for generating custom IDs 'INCREMENTAL' and 'RANDOM'.
Incremental like :
STD-0001 STD-0002 USR-0003 ..., while reset on prefix change is false
STD-0001 STD-0002 USR-0001 ..., while reset on prefix change is true
Random like :
STD-4a67 STD-cc32 STD-19B5 ..., using 'ALPHA_NUMERIC' characters
STD-9458 STD-3498 STD-7453 ..., using 'NUMERIC' characters
STD-agKe STD-BvtM STD-crrQ ..., using 'ALPHA' characters
Each method must have model, column, length, prefix
model : your model class, Ex : Student::class.
column: Optional, Default is Model KeyName.
length: ID length. Optional, Default is 10.
prefix: Define your prefix. It can be a year, month or any custom letters. Optional.
N.B, if the 'column' type is (int, integer, bigint or numeric) the 'prefix' must be numeric as well.
With the addition of the following option in 'INCREMENTAL' method
reset_on_prefix_change: Optional, default true. If you want reset column value to 1 on prefix change then set it true.
Or with the addition of the following options in 'RANDOM' method
set: Optional, default 'ALPHA_NUMERIC'. is a set of characters used for generating random value.
extra: Optional, add extra characters to set option.
IdCustomizer::generate(
[
'model' => Student::class,
'length' => 8,
'prefix' => 'STD-'
]
);
IdCustomizer::generate(
[
'model' => Invoice::class,
'length' => 9,
'prefix' => date('ym')
]
);
Out put => 220300001
IdCustomizer::generate(
[
'model' => User::class,
'column' => 'uid',
'length' => 10,
'prefix' => date('ym')
]
);
Out put => 2203000001
IdCustomizer::generate(
[
'model' => User::class,
'column' => 'uid',
'length' => 10,
'prefix' => date('ym'),
'reset_on_prefix_change' => false
]
);
Out put => 2204000002
IdCustomizer::generate(
[
'factory_method' => IdCustomizer::RANDOM,
'model' => Media::class,
'length' => 10,
'prefix' => 'IMG-',
'set' => IdCustomizer::ALPHA_NUMERIC
]
);
Out put => IMG-k9C42a
IdCustomizer::generate(
[
'factory_method' => IdCustomizer::RANDOM,
'model' => User::class,
'length' => 13,
'prefix' => 'USR-'.date('ym'),
'set' => IdCustomizer::NUMERIC
]
);
Out put => USR-22035673
set.IdCustomizer::generate(
[
'factory_method' => IdCustomizer::RANDOM,
'model' => User::class,
'column' => 'uid',
'length' => 10,
'prefix' => 'U-'
'set' => 'ABCDabcd1234'
]
);
Out put => U-Ac32Dbb4
set.IdCustomizer::generate(
[
'factory_method' => IdCustomizer::RANDOM,
'model' => User::class,
'column' => 'uid',
'length' => 10,
'prefix' => 'S-'
'set' => IdCustomizer::NUMERIC,
'extra' => 'abcdef'
]
);
Out put => S-f059ba01
How can I help you explore Laravel packages today?