Laravel Uuid

Installation

Add webpatser/laravel-uuid to composer.json.

"webpatser/laravel-uuid": "^2.0"

Run composer update to pull down the latest version of Laravel UUID.

Or install it directly from the command line using

composer require "webpatser/laravel-uuid:2.*"

In Laravel 5.5 laravel-uuid will install via the new Package Discovery feature

For Laravel <= 5.4: edit config/app.php and add the provider and alias


'providers' => [
    // ommited
    
    \Webpatser\Uuid\UuidServiceProvider::class,
]
    
'aliases' => [
    // ommited
    
    'Uuid' => Webpatser\Uuid\Uuid::class,
]

For Laravel 4: edit app/config/app.php and add the alias

'aliases' => array(
    // ommited
    'Uuid' => 'Webpatser\Uuid\Uuid',
)

Basic Usage

To quickly generate a UUID just do

Uuid::generate()

This will generate a version 1 with a random ganerated MAC address.

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.

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

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

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

Generate a version 4, truly random, UUID

Uuid::generate(4);

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

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

Some magic features

To import a UUID

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

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

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

Extract the version of an UUID

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

Validation

Just use like any other Laravel validator.

'uuid-field' => 'uuid'

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

$uuid = Uuid::generate();
$validator = Validator::make(['uuid' => $uuid], ['uuid' => 'uuid']);
dd($validator->passes());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值