alsofronie/eloquent-uuid
Composer 安装命令:
composer require alsofronie/eloquent-uuid
包简介
A Laravel Eloquent Model trait for using UUID's as primary keys
README 文档
README
An Eloquent UUID Trait to use with Laravel 5.1 - 5.4
It should work with Laravel 5.0 also, but it's untested.
The trait overwrites the static boot method and listens to the creating
event. It generates a UUID (strips the dashes) and stores it in the primary
key attribute. Thus, you'll need a CHAR(32) primary key for your model
(see migrations below).
Installation
composer require alsofronie/eloquent-uuid:dev-master
Use
In order to make it faster, you have the option to use one of three traits:
UuidModelTrait- the key must beCHAR(36)and contains the dashesUuid32ModelTrait- the key must beCHAR(32), the dashes are strippedUuidBinaryModelTrait- the key isBINARY(16).
Using UuidModelTrait
In order to use this trait, your schema must be something like:
<?php
// ...
Schema::create('users', function (Blueprint $table) {
$table->uuid('id'); // this will create a CHAR(36) field
// or
// $table->char('id', 36);
$table->string('username', 32);
$table->string('password', 50);
// ...
$table->primary('id');
});
Using Uuid32ModelTrait
For this type, just use CHAR(32) in your schema (this is identical to the first one, but with stripped dashes).
<?php
// ...
Schema::create('users', function (Blueprint $table) {
$table->char('id', 32);
// ...
$table->string('username', 32);
$table->string('password', 50);
$table->primary('id');
});
Using UuidBinaryModelTrait
This stores the key as binary. The default Laravel Blueprint curretly
does not currently support binary fields with specified length,
and (at least in MySQL) you cannot create an index (including primary key) on a BINARY field without length.
So, the schema definition should be something like this (please double check if you're not using MySQL):
<?php
// ...
Schema::create('users', function (Blueprint $table) {
$table->string('username', 32);
$table->string('password', 50);
});
DB::statement('ALTER TABLE `usersb` ADD `id` BINARY(16); ALTER TABLE `usersb` ADD PRIMARY KEY (`id`);')
?>
There are two additional notes for this particular trait.
Note 1. In order to get a string representation of your uuid, simple call
$model->id_stringand you'll get it.
Note 2. You can use
User::find($uuid)with both the binary version or the string (bin2hex) version.
Using the optimized uuid
To use the optimized uuid, put the following line in your models:
private static $uuidOptimization = true;
In your models
In order to use this in your models, just put use Uuid[32|Binary]ModelTrait;:
<?php
namespace App;
use Alsofronie\Uuid\Uuid[32|Binary]ModelTrait;
class User extends Eloquent
{
use Uuid[32|Binary]ModelTrait;
}
Running tests
To run the tests, just run composer install and ./vendor/bin/phpunit.
alsofronie/eloquent-uuid 适用场景与选型建议
alsofronie/eloquent-uuid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 336.11k 次下载、GitHub Stars 达 94, 最近一次更新时间为 2015 年 11 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 alsofronie/eloquent-uuid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alsofronie/eloquent-uuid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 336.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 94
- 点击次数: 27
- 依赖项目数: 12
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-16