mauro-pinheiro / eloquent-uuid
为Eloquent模型自动添加UUID生成支持。
2.4.0
2020-09-09 01:55 UTC
Requires
- php: ^7.1
- ramsey/uuid: ^3.9 || ^4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ^3.5 || ^4.0 || ^5.0 || ^6.0
- phpunit/phpunit: ^7.0 || ^8.0 || ^9.0
README
Eloquent UUID
这是一个简单灵活的Laravel扩展包,为任何Eloquent模型自动添加UUID生成支持。
- 自动生成
uuid。 - 为表中的
uuid列选择自定义名称。 (默认 'uuid') - 选择生成的uuid版本。 (默认 '4')
- 检查
uuid版本和列名。 (抛出InvalidUuidVersionException和MissingUuidColumnException异常) - 防止更新uuid值。
什么是UUID?
通用唯一标识符(UUID)是用于在计算机系统中标识信息的128位数字。它是一个由32个字母数字字符组成的36个字符长的标识符,其中包含四个连字符。例如:123E4567-E89b-12D3-A456-426655440000,包含字母和数字,可以唯一标识某个东西。你可以在这里了解更多。
安装
您可以通过Composer安装此包
composer require your-app-rocks/eloquent-uuid
或通过 composer.json 文件
{
"require": {
"your-app-rocks/eloquent-uuid": "~2"
}
}
使用方法
创建表
创建带有 uuid 列的表。例如
<?php Schema::create('users', function (Blueprint $table) { $table->uuid('uuid'); $table->string('name'); $table->timestamps(); });
创建模型
在你的Eloquent模型中,添加特质 HasUuid
<?php namespace App\YourNameSpace; use Illuminate\Database\Eloquent\Model; use YourAppRocks\EloquentUuid\Traits\HasUuid; class User extends Model { use HasUuid; }
创建控制器
<?php namespace App\YourNameSpace; use App\YourNameSpace\User; use Illuminate\Http\Request; class UserController extends Controller { /** * When a new record is inserted into the table `(with the create() or save() methods)`, * Trait "HasUuid" will automatically generate a uuid version 4* for the 'uuid' column of your schema. */ public function store(Request $request) { $user = User::create($request->all()); // Automatically generate a uuid return $user->getUuid() // Return UUID value. } /** * Get User by custom 'UUID' key name - Implicit Binding. * See https://laravel.net.cn/docs/5.8/routing#route-model-binding * * @param User $user * @return void */ public function show(User $user) { return $user; } //OR /** * Get User by scope query. */ public function show($uuid) { $user = User::findByUuid($uuid); return $user; } }
自定义配置
您可以通过更改 列名 和 uuid版本 来轻松配置此包以符合您的需求。例如
<?php //Create table Schema::create('posts', function (Blueprint $table) { $table->uuid('universally_unique_id'); $table->string('title'); $table->timestamps(); }); //Eloquent Model class Post extends Model { use HasUuid; protected $uuidColumnName = 'universally_unique_id'; protected $uuidVersion = 1; // Available 1,3,4 or 5 protected $uuidString = ''; // Needed when $uuidVersion is "3 or 5" }
高级自定义配置
此包旨在灵活且易于自定义!
您可以使用特质 Uuidable 来创建具有自定义代码的自己的特质。
方法
YourAppRocks\EloquentUuid\Traits\Uuidable;
- getUuidColumnName()
// 获取列名。(默认 'uuid') - getUuid()
// 获取uuid值。 - setUuid($value)
// 设置uuid值。 - generateUuid()
// 生成UUID值。(使用Ramsey\Uuid) - getUuidVersion()
// 获取uuid版本或默认为4。 - getUuidString()
// 获取用于生成uuid版本3和5的字符串。 - validateUuidVersion()
// 验证uuid版本。
示例自定义代码
将特质 HasUuid 替换为 MyUuidTrait
<?php //Create table Schema::create('users', function (Blueprint $table) { $table->uuid('uuid'); $table->string('name'); $table->timestamps(); }); //Create MyUuidTrait with custom code use YourAppRocks\EloquentUuid\Traits\Uuidable; trait MyUuidTrait { use Uuidable; /** * Boot trait on the model. * * @return void */ public static function bootMyUuidTrait() { static::creating(function ($model) { // My custom code here. }); static::saving(function ($model) { // My custom code here. }); } // My custom code here. } //Create Model use MyUuidTrait; use Illuminate\Database\Eloquent\Model; class User extends Model { use MyUuidTrait; }
变更日志
请参阅CHANGELOG 了解最近更改的详细信息。
测试
$ composer test
贡献
请参阅CONTRIBUTING 了解详细信息。
鸣谢
此包受到此 包的启发。
许可协议
MIT许可协议(MIT)。请参阅许可文件 了解更多信息。
