承接 wujunze/laravel-id-generate 相关项目开发

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

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

wujunze/laravel-id-generate

Composer 安装命令:

composer require wujunze/laravel-id-generate

包简介

Laravel package to generate and to validate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in. and generate number id, generate primary key

README 文档

README

Build Status codecov.io Latest Stable Version Licence Total Downloads

Laravel package to generate and to validate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in. and generate number id, generate primary key

Base on laravel-uuid

Thanks to laravel-uuid

Installation

In Laravel 5.5 laravel-uuid will install via the new package discovery feature so you only need to add the package to your composer.json file

composer require "wujunze/laravel-id-generate"

after installation you should see

Discovered Package: wujunze/laravel-id-generate

and you are ready to go

Basic Usage

To quickly generate a UUID just do

IdGen::generate()

This will generate a version 1 IdGen object with a random generated MAC address.

To echo out the generated UUID, cast it to a string

(string) IdGen::generate()

or

IdGen::generate()->string

Advanced Usage

UUID creation

Generate a version 1, time-based, UUID. You can set the optional node to the MAC address. If not supplied it will generate a random MAC address.

IdGen::generate(1,'00:11:22:33:44:55');

Generate a version 3, name-based using MD5 hashing, UUID

IdGen::generate(3,'test', IdGen::NS_DNS);

Generate a version 4, truly random, UUID

IdGen::generate(4);

Generate a version 5, name-based using SHA-1 hashing, UUID

IdGen::generate(5,'test', IdGen::NS_DNS);

id generate

Generate sample primary key

 IdGen::getSamplePk();

Generate id by type and share key

 IdGen::genIdByTypeShareKey(6,89);

Generate id by type

 IdGen::genIdByType(8);

Generate code

 IdGen::genCode(9, 9, 888);

Generate SnowFlake Id

 IdGen::snowFlakeId();

Some magic features

To import a UUID

$uuid = IdGen::import('d3d29d70-1d25-11e3-8591-034165a3a613');

Extract the time for a time-based UUID (version 1)

$uuid = IdGen::generate(1);
dd($uuid->time);

Extract the version of an UUID

$uuid = IdGen::generate(4);
dd($uuid->version);

Eloquent UUID generation

If you want an UUID magically be generated in your Laravel models, just add this boot method to your Model.

/**
 *  Setup model event hooks
 */
public static function boot()
{
    parent::boot();
    self::creating(function ($model) {
        $model->uuid = (string) IdGen::generate(4);
    });
}

This will generate a version 4 UUID when creating a new record.

Model binding to UUID instead of primary key

If you want to use the UUID in URLs instead of the primary key, you can add this to your model (where 'uuid' is the column name to store the UUID)

/**
 * Get the route key for the model.
 *
 * @return string
 */
public function getRouteKeyName()
{
    return 'uuid';
}

When you inject the model on your resource controller methods you get the correct record

public function edit(Model $model)
{
   return view('someview.edit')->with([
        'model' => $model,
    ]);
}

Validation

Just use like any other Laravel validator.

'uuid-field' => 'uuid' 'uuid-field' => 'gen_id'

Or create a validator from scratch. In the example an IdGen object in validated. You can also validate strings $uuid->string, the URN $uuid->urn or the binary value $uuid->bytes

$uuid = IdGen::generate();
$genId=  IdGen::getSamplePk();
$validator = Validator::make(['uuid' => $uuid], ['uuid' => 'uuid'], ['gen_id' => $genId]);
dd($validator->passes());

Notes

Full details on the UUID specification can be found on http://tools.ietf.org/html/rfc4122.

Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees.

Inspire And Thanks

Snowflake php implement

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固