Laravel UUID生成
Laravel UUID generation
我正在尝试使用 laravel-uuid 包生成一个 UUID(不是主键,只是生成一个)。文档非常简单,所以根据自述文件,UUID 应该通过调用 $uuid = Uuid::generate();
生成,但它 returns 是一个空对象。 (我也试过了$uuid = Uuid::generate(1);
)
我按照那里提供的安装说明进行操作(没有异常),应用程序没有抛出任何错误,所以我想一切正常。
也欢迎使用其他软件包。
$uuid
可能是空的,因为您的系统没有提供正确类型的熵。您可以为 v4 或 v5 UUID 尝试这些库实现:
// https://tools.ietf.org/html/rfc4122#section-4.4
function v4() {
$data = openssl_random_pseudo_bytes(16, $secure);
if (false === $data) { return false; }
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
// https://tools.ietf.org/html/rfc4122#section-4.3
function v5($name) {
$hash = sha1($name, false);
return sprintf(
'%s-%s-5%s-%s-%s',
substr($hash, 0, 8),
substr($hash, 8, 4),
substr($hash, 17, 3),
substr($hash, 24, 4),
substr($hash, 32, 12)
);
}
原来我必须使用 $uuid->string
来获取实际 ID,如果您尝试在 json 响应中 return 整个对象显示为空。
尝试使用这个包会在你的模型中自动生成和分配UUID字段,也可以通过UUIDs键显示和更新。
在 laravel 5.6 之后添加了一个新的助手来生成通用唯一标识符 (UUID)
use Illuminate\Support\Str;
return (string) Str::uuid();
return (string) Str::orderedUuid();
方法return一个Ramsey\Uuid\Uuid
对象
orderedUuid()
方法将生成时间戳优先 UUID,以便更轻松、更高效地建立数据库索引。
在Laravel 5.6+
use Illuminate\Support\Str;
$uuid = Str::uuid()->toString();
在 table 名称中添加列 'Uuid',键入 'char' 长度 36
在 Models 文件夹中创建文件夹名称 'Traits'
在 Traits 文件夹中创建文件名 Uuid.php
Uuid.php
<?php
namespace App\Models\Traits;
use Ramsey\Uuid\Uuid as PackageUuid;
trait Uuid
{
public function scopeUuid($query, $uuid)
{
return $query->where($this->getUuidName(), $uuid);
}
public function getUuidName()
{
return property_exists($this, 'uuidName') ? $this->uuidName : 'uuid';
}
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->{$model->getUuidName()} = PackageUuid::uuid4()->toString();
});
}
}
- 在模型中添加 'use Uuid'
对于 laravel < 5.6
使用DB::raw('uuid()');
请看下面的例子
模型内部
use Illuminate\Support\Facades\DB;
public static function boot(){
parent::boot();
$creationCallback = function ($model) {
if (empty($model->{$model->getKeyName()}))
$model->{$model->getKeyName()} = DB::raw('uuid()');
};
static::creating($creationCallback);
}
优点
--如果您使用 laravel < 5.6
,则不必使用任何 third-party 插件来获取 UUID
缺点
使用 ORM 保存数据后,您需要再次查询以获取最后插入的 ID,否则您将获取 uuid()。
我正在尝试使用 laravel-uuid 包生成一个 UUID(不是主键,只是生成一个)。文档非常简单,所以根据自述文件,UUID 应该通过调用 $uuid = Uuid::generate();
生成,但它 returns 是一个空对象。 (我也试过了$uuid = Uuid::generate(1);
)
我按照那里提供的安装说明进行操作(没有异常),应用程序没有抛出任何错误,所以我想一切正常。
也欢迎使用其他软件包。
$uuid
可能是空的,因为您的系统没有提供正确类型的熵。您可以为 v4 或 v5 UUID 尝试这些库实现:
// https://tools.ietf.org/html/rfc4122#section-4.4
function v4() {
$data = openssl_random_pseudo_bytes(16, $secure);
if (false === $data) { return false; }
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
// https://tools.ietf.org/html/rfc4122#section-4.3
function v5($name) {
$hash = sha1($name, false);
return sprintf(
'%s-%s-5%s-%s-%s',
substr($hash, 0, 8),
substr($hash, 8, 4),
substr($hash, 17, 3),
substr($hash, 24, 4),
substr($hash, 32, 12)
);
}
原来我必须使用 $uuid->string
来获取实际 ID,如果您尝试在 json 响应中 return 整个对象显示为空。
尝试使用这个包会在你的模型中自动生成和分配UUID字段,也可以通过UUIDs键显示和更新。
在 laravel 5.6 之后添加了一个新的助手来生成通用唯一标识符 (UUID)
use Illuminate\Support\Str;
return (string) Str::uuid();
return (string) Str::orderedUuid();
方法return一个Ramsey\Uuid\Uuid
对象
orderedUuid()
方法将生成时间戳优先 UUID,以便更轻松、更高效地建立数据库索引。
在Laravel 5.6+
use Illuminate\Support\Str;
$uuid = Str::uuid()->toString();
在 table 名称中添加列 'Uuid',键入 'char' 长度 36
在 Models 文件夹中创建文件夹名称 'Traits'
在 Traits 文件夹中创建文件名 Uuid.php
Uuid.php
<?php
namespace App\Models\Traits;
use Ramsey\Uuid\Uuid as PackageUuid;
trait Uuid
{
public function scopeUuid($query, $uuid)
{
return $query->where($this->getUuidName(), $uuid);
}
public function getUuidName()
{
return property_exists($this, 'uuidName') ? $this->uuidName : 'uuid';
}
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->{$model->getUuidName()} = PackageUuid::uuid4()->toString();
});
}
}
- 在模型中添加 'use Uuid'
对于 laravel < 5.6
使用DB::raw('uuid()');
请看下面的例子
模型内部
use Illuminate\Support\Facades\DB;
public static function boot(){
parent::boot();
$creationCallback = function ($model) {
if (empty($model->{$model->getKeyName()}))
$model->{$model->getKeyName()} = DB::raw('uuid()');
};
static::creating($creationCallback);
}
优点
--如果您使用 laravel < 5.6
,则不必使用任何 third-party 插件来获取 UUID缺点
使用 ORM 保存数据后,您需要再次查询以获取最后插入的 ID,否则您将获取 uuid()。