cloudcake/laravel-uuid

Laravel Eloquent 模型的 UUID。

v1.5 2020-04-08 08:26 UTC

This package is auto-updated.

Last update: 2024-09-27 05:59:33 UTC


README

自 Laravel 9.x 版本以来,Eloquent 的 UUID 支持已直接集成到 Laravel 中。
请参阅 Illuminate/Database/Eloquent/Concerns/HasUuids

此包现在是冗余的。

安装

使用 composer 通过 composer require cloudcake/laravel-uuid 安装

用法

主键 UUID

如果您想将 UUID 用作模型的唯一键,请参考以下内容。

  • Uuid\Traits\UuidPrimaryKey 特性添加到 Eloquent 模型中
  • 如果您不是使用默认的 id 列名,请添加主键列名(可选)
use Uuid\Traits\UuidPrimaryKey;

class User extends Authenticatable
{
    use UuidPrimaryKey;

    protected $primaryKey = 'id';
}

现在您可以使用 User::find('<uuid-here>'); 调用。

附加字段 UUID

在某些情况下,您可能希望保留常规的基于整数的唯一键,但为模型添加一个额外的 UUID 字段,对于这种情况,请使用 Uuid 特性。

  • Uuid\Traits\Uuid 特性添加到 Eloquent 模型中
  • 添加 UUID 字段的名称
use Uuid\Traits\Uuid;

class User extends Authenticatable
{
    use Uuid;

    protected static $uuidKeyName = 'uuid';
}

然后像查询任何其他属性一样查询它,User::where('uuid', '<uuid-here>')->first()

附加选项

将以下任何一个或所有选项添加到您的模型中,以调整包以满足您的需求。

示例

use Uuid\Traits\Uuid;
use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    use Uuid;

    /**
     * The name of the UUID column in the database.
     *
     * @var string
     */
    protected static $uuidKey = 'universally_unique_id';

    /**
     * Whether or not the UUID should use timestamp ordering.
     *
     * @var bool
     */
    protected static $uuidOrdered = true;
}