your-app-rocks/eloquent-uuid

为 Eloquent 模型添加自动生成 UUID 的支持。

2.5.0 2021-02-25 01:21 UTC

This package is auto-updated.

Last update: 2024-09-25 09:58:26 UTC


README

YourApp.Rocks logo

Eloquent UUID

Software License Build Status Latest Stable Version Total Downloads

一个简单灵活的 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 模型中添加 trait 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"
}

高级自定义

此包是为了灵活性和易于自定义而构建的!

您可以使用 trait Uuidable 创建自己的 trait 并包含自定义代码。

方法

YourAppRocks\EloquentUuid\Traits\Uuidable;

  • getUuidColumnName() // 获取列名。 (默认 'uuid' )
  • getUuid() // 获取 uuid 值。
  • setUuid($value) // 设置 uuid 值。
  • generateUuid() // 生成 UUID 值。 (使用 Ramsey\Uuid )
  • getUuidVersion() // 获取 uuid 版本或默认为 4。
  • getUuidString() // 获取生成 uuid 版本 3 和 5 的字符串。
  • validateUuidVersion() // 验证 uuid 版本。

示例自定义代码

将 trait 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)。请参阅 许可证文件 了解更多信息。