ddlabs/uuid-package

一个允许你在Laravel模型中使用UUID作为主键的包

2 2021-06-22 00:32 UTC

This package is auto-updated.

Last update: 2024-09-18 18:24:34 UTC


README

DiegoDev Labs

DiegoLabs UUID Package

一个简单的包,允许你通过特质将UUID添加到Laravel模型中。

安装

 composer require ddlabs/uuid-package

特质

  • SetsUuidWhenCreating - 这个特质将模型的主键设置为UUID。如果你有一个默认创建UUID的工作流程,则允许你传递一个UUID。如果UUID符合版本4规范,则特质将直接使用传递给它的UUID。我们有时会在记录在移动设备上创建并通过API传递到后端时使用这个。它们可能已经与UUID相关联。

用法

数据库中必须配置ID以接受UUID。

对于迁移

public function up()
{
    Schema::create('your_models', function (Blueprint $table) {
        $table->uuid('id'); // Set to uuid
        $table->string('name');
        $table->timestamps();
        $table->primary('id'); // Add primary key
    });
}

对于模型

use Ddlabs\Uuid\Traits\SetsUuidWhenCreating;

class YourModel extends Model
{
    use SetsUuidWhenCreating;

    public $incrementing = false;

    protected $keyType = 'string';
   ...
}