我想使用laravel-uuid包生成UUID(非主键,仅生成一个)。根据自述文件所述,文档非常简单,只需调用$uuid = Uuid::generate();
即可生成UUID,但它返回一个空对象。(我还尝试过$uuid = Uuid::generate(1);
)
我按照提供的安装说明进行了安装(没有任何异常情况),应用程序没有抛出任何错误,因此我认为一切都没问题。
也欢迎使用其他替代方案。
我想使用laravel-uuid包生成UUID(非主键,仅生成一个)。根据自述文件所述,文档非常简单,只需调用$uuid = Uuid::generate();
即可生成UUID,但它返回一个空对象。(我还尝试过$uuid = Uuid::generate(1);
)
我按照提供的安装说明进行了安装(没有任何异常情况),应用程序没有抛出任何错误,因此我认为一切都没问题。
也欢迎使用其他替代方案。
在 Laravel 5.6 之后,新增了一个帮助函数用于生成通用唯一标识符(UUID)。
use Illuminate\Support\Str;
return (string) Str::uuid();
return (string) Str::orderedUuid();
这些方法返回一个Ramsey\Uuid\Uuid
对象
orderedUuid()
方法将生成一个以时间戳为基础的UUID,以便更轻松、更高效地进行数据库索引。
In Laravel 5.6+
use Illuminate\Support\Str;
$uuid = Str::uuid()->toString();
$uuid->string
才能获取实际的ID,在json响应中尝试返回整个对象会显示为空。protected $casts = ['id' => 'string'];
- Kalema Edgar在 Laravel 9 和 10 中
use Illuminate\Support\Str;
return (string) Str::uuid();
使用 Laravel 9 模型导入时,需要为主键 id 指定一个值。
use Illuminate\Database\Eloquent\Concerns\HasUuids;
然后在你的类中添加
use HasUuids;
可能是因为系统没有提供正确类型的熵,导致$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',类型为'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();
});
}
}
适用于 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,则无需使用任何第三方插件即可获取 UUID。
缺点
使用 ORM 保存数据后,您需要再次查询以获取最后插入的 ID,否则将返回 uuid()。
/**
* Setup model event hooks
*/
public static function boot()
{
parent::boot();
self::creating(function ($model) {
$model->uuid = (string) Str::uuid();
});
}
在迁移文件中
Schema::create('ekankotri.in', function (Blueprint $table) {
$table->id();
$table->uuid('uuid');
});
对于 Laravel 9,您可以使用
use Illuminate\Support\Str;
return (string) Str::uuid();
echo Uuid::generate()
或$uuid = Uuid::generate(); echo $uuid->string
。 - Ben Swinburne以获取v1 UUID。或者使用
->string`获取字符串版本。 - bishop