承接 hexify/laravel-id-customizer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

  • Using it in controller :

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();
}
  • Using it in model directly :

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-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.

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 hexify/laravel-id-customizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-31