adamlien/pkuuids

使用 UUID 作为 laravel 5.3+ 的主键

v1.6 2023-03-18 12:16 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:14:53 UTC


README

PkUuids 包使用 ramsey/uuid[https://github.com/ramsey/uuid] (在 5.3+ 上官方安装) 生成 UUID_v4(随机) 作为 Eloquent Model 的主键。

灵感来源于 http://stackoverflow.com/questions/31817067/laravel-5-using-a-uuid-as-primary-key

安装

  1. 使用 composer 将包添加到您的项目中,使用 composer require racklin/pkuuids:dev-master

迁移

使用 UUID 作为主键,我们需要将 $table->increments('id'); 更改为 $table->uuid('id')->primary();

模型

禁用自增主键

在您的模型中添加 $incrementing = false;

使用 Traits

在您的模型中添加 use PkUuuids;

示例

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Racklin\PkUuids\PkUuids;

class User extends Model
{
    use PkUuids;

    public $incrementing = false;
}

Laravel 版本

当前包版本适用于 Laravel 5.3+ 6.x 7.x 8.x 9.x 10.x