goldspecdigital/laravel-eloquent-uuid

为Eloquent模型ID提供UUID支持的简单即插即用解决方案。

v10.0.0 2023-02-23 18:49 UTC

README

Eloquent UUID

GitHub stars

GitHub tag (latest SemVer) Build status Packagist PHP from Packagist Packagist

介绍

为Eloquent模型ID提供UUID支持的简单即插即用解决方案。

默认支持v1v4 ID,但如果需要v3v5支持,可以轻松添加。

安装

请参考下表,以确定与您已安装的Laravel版本一起使用的正确版本

您可以通过composer安装此包

composer require goldspecdigital/laravel-eloquent-uuid:^10.0

使用方法

使用此包有两种方式

  1. 通过扩展提供的模型类(推荐且最简单的方法)。
  2. 通过使用提供的模型特质(允许扩展其他模型类)。

扩展模型

在创建Eloquent模型时,而不是扩展标准的Laravel模型类,扩展此包提供的模型类

<?php

namespace App\Models;

use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;

class BlogPost extends Model
{
    //
}

扩展用户模型

随标准Laravel安装附带的用户模型有一些额外的配置,这些配置在父类中实现。此配置仅包括实现几个接口和使用几个特质。

提供了一个即插即用的替代方案,您可以通过扩展此包提供的User类来使用它

<?php

namespace App\Models;

use GoldSpecDigital\LaravelEloquentUUID\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    //
}

使用特质

作为上述示例中扩展类的替代方案,您还可以使用提供的特质。这需要一个更复杂的设置过程,但允许您在需要的情况下从另一个类扩展模型。

<?php

namespace App\Models;

use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Uuid;
use Illuminate\Database\Eloquent\Model;

class BlogPost extends Model
{
    use Uuid;
    
    /**
     * The "type" of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
}

生成UUID

如果您没有指定模型主键的值,将自动生成UUID。但是,如果您指定了您自己的UUID,则不会生成一个,而是使用您明确提供的UUID。这在你需要在创建模型之前知道模型ID时很有用。

// No UUID provided (automatically generated).
$model = Model::create();
echo $model->id; // abb034ae-fcdc-4200-8094-582b60a4281f

// UUID explicity provided.
$model = Model::create(['id' => '04d7f995-ef33-4870-a214-4e21c51ff76e']);
echo $model->id; // 04d7f995-ef33-4870-a214-4e21c51ff76e

指定UUID版本

默认情况下,您的模型将使用v4 UUID。但是,您也可以通过在您的模型上设置以下属性/方法来指定使用v1 UUID。

当扩展类时

<?php

namespace App\Models;

use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;

class BlogPost extends Model
{
    /**
     * The UUID version to use.
     *
     * @var int
     */
    protected int $uuidVersion = 1;
}

当使用特质时

<?php

namespace App\Models;

use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Uuid;
use Illuminate\Database\Eloquent\Model;

class BlogPost extends Model
{
    use Uuid;

    /**
     * The UUID version to use.
     *
     * @return int
     */
    protected function uuidVersion(): int
    {
        return 1;
    }
}

v3v5的支持

如果您需要支持v3v5 UUID,可以简单地重写负责生成UUID的方法。

<?php

namespace App\Models;

use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
use Ramsey\Uuid\Uuid;

class BlogPost extends Model
{
    /**
     * @throws \Exception
     * @return string
     */
    protected function generateUuid(): string
    {
        // UUIDv3 has been used here, but you can also use UUIDv5.
        return Uuid::uuid3(Uuid::NAMESPACE_DNS, 'example.com')->toString();
    }
}

创建模型

除了make:model artisan命令外,您现在还可以使用uuid:make:model,它具有标准make:model命令的所有功能(但不能创建关系模型)。

php artisan uuid:make:model Models/Post --all

数据库迁移

默认迁移中使用的默认主ID列与UUID主键不兼容,因为默认列类型是无符号整数。UUID是36个字符的字符串,因此我们必须在我们的迁移中指定此内容。

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table): void {
            // Primary key.
            $table->uuid('id')->primary();
        });
    }
}

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table): void {
            // Primary key.
            $table->uuid('id')->primary();
        
            // Foreign key.
            $table->uuid('user_id');
            $table->foreign('user_id')->references('id')->on('users');
        });
    }
}

运行测试

要运行测试套件,可以使用以下命令

composer test

贡献

有关我们的行为准则和提交拉取请求的流程的详细信息,请参阅CONTRIBUTING.md

版本控制

我们使用SemVer进行版本控制。有关可用的版本,请参阅此存储库的标签

作者

另请参阅参与此项目的贡献者列表

许可证

本项目采用MIT许可证 - 有关详细信息,请参阅LICENSE.md文件。