hexify/laravel-id-customizer
Composer 安装命令:
composer require hexify/laravel-id-customizer
包简介
A laravel package to customize IDs formats.
README 文档
README
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.
Installation :
composer require hexify/laravel-id-customizer
Usage :
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.
Parameters explanation :
There is two methods for generating custom IDs 'INCREMENTAL' and 'RANDOM'.
-
Incremental like :
STD-0001STD-0002USR-0003..., while reset on prefix change is falseSTD-0001STD-0002USR-0001..., while reset on prefix change is true -
Random like :
STD-4a67STD-cc32STD-19B5..., using 'ALPHA_NUMERIC' charactersSTD-9458STD-3498STD-7453..., using 'NUMERIC' charactersSTD-agKeSTD-BvtMSTD-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 resetcolumnvalue 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 tosetoption.
Examples :
=> Incremental
- Example 01: generating 'STD-0001' ID, prefix is a non numeric value so your column should be varchar type.
IdCustomizer::generate(
[
'model' => Student::class,
'length' => 8,
'prefix' => 'STD-'
]
);
- Example 02: 'YYMM00001' ID.
IdCustomizer::generate(
[
'model' => Invoice::class,
'length' => 9,
'prefix' => date('ym')
]
);
Out put => 220300001
- Example 03: Determine the desired column.
IdCustomizer::generate(
[
'model' => User::class,
'column' => 'uid',
'length' => 10,
'prefix' => date('ym')
]
);
Out put => 2203000001
- Example 04: If you don't want to reset the identifier when the prefix change.
IdCustomizer::generate(
[
'model' => User::class,
'column' => 'uid',
'length' => 10,
'prefix' => date('ym'),
'reset_on_prefix_change' => false
]
);
Out put => 2204000002
=> Random
- Example 01: 'IMG-k9C42a' ID.
IdCustomizer::generate(
[
'factory_method' => IdCustomizer::RANDOM,
'model' => Media::class,
'length' => 10,
'prefix' => 'IMG-',
'set' => IdCustomizer::ALPHA_NUMERIC
]
);
Out put => IMG-k9C42a
- Example 02: 'USR-YYMM****' ID.
IdCustomizer::generate(
[
'factory_method' => IdCustomizer::RANDOM,
'model' => User::class,
'length' => 13,
'prefix' => 'USR-'.date('ym'),
'set' => IdCustomizer::NUMERIC
]
);
Out put => USR-22035673
- Example 03: Define a custom
set.
IdCustomizer::generate(
[
'factory_method' => IdCustomizer::RANDOM,
'model' => User::class,
'column' => 'uid',
'length' => 10,
'prefix' => 'U-'
'set' => 'ABCDabcd1234'
]
);
Out put => U-Ac32Dbb4
- Example 04: Add extra characters to
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
hexify/laravel-id-customizer 适用场景与选型建议
hexify/laravel-id-customizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「id generator」 「id customizer」 「random id」 「prefixed id」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hexify/laravel-id-customizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hexify/laravel-id-customizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hexify/laravel-id-customizer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Easy custom fonts upload and CSS injection with Kirki WordPress plugin.
Easy custom WordPress customizer settings for WordPress MVC (WPMVC) framework.
Adds several Theme Customizer controls
A Range Slider Control for the WordPress Customizer.
A Toggle Control for the WordPress Customizer.
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-31