sirajcse / laravel-unique-id-generator
此包的最新版本(v1.0.1)没有可用的许可证信息。
在 Laravel 框架中生成自定义唯一 ID 或代码(带前缀或后缀或两者或仅唯一 ID)或更改前缀或后缀或两者后重置您的 ID
v1.0.1
2022-05-12 17:25 UTC
Requires
- php: >=5.6.0
README
##生成自定义唯一 ID 或代码(带前缀或后缀或两者或仅唯一 ID)或更改前缀或后缀或两者后重置您的 ID 在 Laravel 框架中
安装
composer require sirajcse/laravel-unique-id-generator
如何使用它?您可以在控制器代码中使用它,作为一个辅助程序或通过定义一个启动方法在您的模型内部。
@@唯一 ID/代码生成器@@@
Inv-000001/12/21
'table' => 'invoices' [sting table name]
'field'=>'invoice_id' [Default 'id'] [Optional][any string field name]
'length' => 12 [Integer value Id length]
'prefix'=>'Inv-' [Default ''] [Optional] [any string]
'suffix'=>date('/m/y') [Default ''] [Optional][any string]
'reset_on_change'=>false[ Default false] [Optional] [Options are 1.prefix , 2.suffix 3.both 4.false]
uniqueId=000001
默认字段 默认字段值 id;[如 'field'=>'id'] 如果需要在表中使用 'code' 字段;'field'=>'code'
前缀或后缀默认值 Prefix=>'' 或 Suffix=>'';
更改时重置 如果您希望在每次更改前缀或后缀或两者时重置值,则设置此值;[reset_on_change 有 4 个选项 prefix、suffix、both、false] 默认值 reset_on_change= false;
控制器示例
Import ID generator for Prefix
use sirajcse\UniqueIdGenerator\UniqueIdGenerator;
前缀示例
public function store(Request $request){
$id = UniqueIdGenerator::generate(['table' => 'todos', 'length' => 6, 'prefix' => date('y')]);
$todo = new Todo();
$todo->id = $id;
$todo->title = $request->get('title');
$todo->save();
}
// 210001 210002 220003 230004
后缀示例
public function store(Request $request){
$id = UniqueIdGenerator::generate(['table' => 'todos', 'length' => 6, 'suffix' => date('y')]);
$todo = new Todo();
$todo->id = $id;
$todo->title = $request->get('title');
$todo->save();
}
// 000121 000221 000322 000422
前缀和后缀示例
public function store(Request $request){
$id = UniqueIdGenerator::generate(['table' => 'todos', 'length' => 10,'prefix' => 'Inv-', 'suffix' => date('y')]);
$todo = new Todo();
$todo->id = $id;
$todo->title = $request->get('title');
$todo->save();
}
// Inv-000121 Inv-000221 Inv-000322 Inv-000422
注意:如果您要生成到表 id 字段的 ID,则必须在您的模型中将 id 字段设置为可填充的,并将 public $incrementing = false; 设置为 true。
模型示例
在您的模型中添加一个启动方法。当添加新记录时,ID 将自动生成。如果需要,为 'prefix'、'suffix' 添加关联数组
public static function boot() {
parent::boot();
self::creating(function ($model) {
$model->uuid = UniqueIdGenerator::generate(['table' => $this->table, 'length' => 10,'prefix' =>'Inv-', 'suffix' =>date('y')]);
});
}
// Inv-000121 Inv-000221 Inv-000322 Inv-000422
参数
您必须在 generate 函数中传递一个包含 table、length、prefix、suffix 键的关联数组。
table = Your table name. Like 'users','invoice'
field = Optional. By default, it works on the id field. You can set other field names also.like 'code', 'invoice_id', 'student_uuid'
length = Your ID/Code length is total length **[ length= prefix+ unique id + suffif ]**
prefix = Optional. By default, it prefix is empty. Define your prefix if your need. It can be a year prefix, month ,any custom letter or empty.
suffix = Optional. By default, it suffix is empty. Define your suffix if your need. It can be a year suffix, month or any custom letter or empty
reset_on_change = Optional, default false. If you want to reset id from 1 on prefix,suffix or both changes then set it prefix,suffix,both,false.
'reset_on_change'=>'prefix' Only Changes of prefix value reset
'reset_on_change'=>'suffix' Only Changes of suffix value reset
'reset_on_change'=>'both' Changes of both prefix and suffix value reset
'reset_on_change'=>'false' Optional. By default, it reset_on_change is false.
示例:01
$config = [ 'table' => 'todos', 'length' => 6, 'prefix' => date('y') 'suffix' => '' ];
// now use it $id = UniqueIdGenerator::generate($coFnfig);
示例 02:仅唯一 ID/代码,无前缀、后缀
// 单行代码内使用
$id = UniqueIdGenerator::generate(['table' => 'todos', 'length' => 6]);
// output: 000001, 0000002
示例 03:INV-000001 为前缀字符串。
您的字段必须是 varchar。
$id = UniqueIdGenerator::generate(['table' => 'invoices', 'length' => 10, 'prefix' =>'INV-']);
//output: INV-000001 ,INV-000002
示例 04:000001/2021 为后缀字符串。
$id = UniqueIdGenerator::generate(['table' => 'invoices', 'length' => 10, 'suffix' =>date('/Y')]);
//output: 00001/2021, 00002/2021
示例 05:INV-000001/2021 为前缀、后缀(两者)字符串。
`$id = UniqueIdGenerator::generate(['table' => 'invoices', 'length' => 14,'prefix' =>'INV-', 'suffix' =>date('/Y')]);
//output: INV-00001/2021, INV-00002/2021`
示例 06:默认(ID 字段),
此包在 ID 字段上工作。您可以将另一个字段设置为生成 ID。确保您选择字段必须是唯一的,并且也是适当的数据类型。
$id = UniqueIdGenerator::generate(['table' => 'products','field'=>'pid', 'length' => 6, 'prefix' =>date('P')]);
//output: P00001
示例 07:默认(重置您的 ID),
This package won't reset your ID when you change your prefix,suffix or Both of ID.
If you want to reset your ID from 1 on every prefix changes then pass reset_on_change => 'prefix'
If you want to reset your ID from 1 on every suffix changes then pass reset_on_change => 'suffix'
If you want to reset your ID from 1 on every prefix and suffix (both) changes then pass reset_on_change => 'both'___
示例 08:每年重置前缀 ID
$id = UniqueIdGenerator::generate(['table' => 'invoices', 'length' => 10, 'prefix' =>date('y'), 'reset_on_change'=>'prefix']);
//output: 2000000001,2000000002,2000000003
//output: 2100000001,2100000002,2100000003
示例 09:每月重置 ID
$id = UniqueIdGenerator::generate(['table' => 'invoices', 'length' => 10, 'prefix' =>date('ym'),, 'reset_on_change'=>'prefix']]);
//output: 1912000001,1912000002,1912000003
//output: 2001000001,2001000002,2001000003
示例 10:或任何前缀更改
$id = UniqueIdGenerator::generate(['table' => 'products', 'length' => 6, 'prefix' => $prefix, 'reset_on_change'=>'prefix']]);
//output: A00001,A00002,B00001,B00002
示例 11:每年重置后缀 ID
`$id = UniqueIdGenerator::generate(['table' => 'invoices', 'length' => 10, 'suffix' =>date('y'),'reset_on_change'=>'suffix']]);
//output: 0000000120,0000000220,0000000320
//output: 0000000121,0000000221,0000000321`
示例 12:每年重置前缀和后缀(两者)ID
$prefix='INV'; $prefix='Pro';
$id = UniqueIdGenerator::generate(['table' => 'invoices', 'length' => 13, 'prefix' =>$prefix,'suffix' =>date('y'),'reset_on_change'=>'both']]);
//output: INV0000000120,INV0000000220,INV0000000320
//output: Pro0000000121,Pro0000000221,Pro0000000321