michalkortas/laravel-uuid

只需创建带有 UUID/GUID 主键的 Eloquent 模型及数据库表。

v1.0.4 2021-03-03 10:08 UTC

This package is auto-updated.

Last update: 2024-09-29 05:56:23 UTC


README

只需创建带有 UUID/GUID 主键的 Eloquent 模型及数据库表。

安装

composer require michalkortas/laravel-uuid

用法

在您的表迁移中添加 uuid 作为主键。

Schema::create('customers', function (Blueprint $table) {
    $table->uuid('id')->primary();
});

将特性添加到您的 Eloquent 模型中。

<?php

namespace AppModels;

use michalkortas\LaravelUuid\traits\HasUuid;
use Illuminate\Database\Eloquent\Model;

class Customers extends Model
{
    use HasUuid;
}

现在,当您运行迁移时,新创建的表的数据类型 ID 为 CHAR(36)。使用 Model::create() 方法时,UUID 将自动插入。

ID datatype - UUID - CHAR(36)