laravolt/eloquent-uuid

为Laravel Eloquent提供通用唯一标识符(UUID)

0.1.0 2015-11-19 00:51 UTC

This package is auto-updated.

Last update: 2024-09-05 19:12:03 UTC


README

在创建/插入新数据时自动生成UUID。

安装

通过Composer

$ composer require laravolt/eloquent-uuid

然后注册服务提供者,前往您的 config/app.php 文件,并在 providers 数组中添加以下行

Laravolt\Database\Eloquent\UuidServiceProvider::class,

用法

创建/修改数据库表列类型

Schema::create('users', function (Blueprint $table) {
    
    // Create UUID column
    $table->char('id', 32)->primary();

    $table->string('name');
});

在Eloquent模型中实现

<?php

namespace App;

use Laravolt\Contracts\Eloquent\Uuid as UuidContract;
use Laravolt\Database\Eloquent\Uuid;

class Book extends Model implements UuidContract
{
    use Uuid;

    // Uuid Columns
    protected $uuid = ['id'];