racklin/pkuuids

使用UUID作为laravel 5.3+的主键

dev-master 2017-04-18 04:37 UTC

This package is auto-updated.

Last update: 2024-09-04 11:39:36 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;

使用特性

在您的模型中添加use PkUuuids;

示例

<?php

namespace App;

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

class User extends Model
{
    use PkUuids;

    $incrementing = false;
}

Laravel版本

当前包版本适用于Laravel 5.3+。