I am trying to generate a UUID (not as primary key, just generate one) with the laravel-uuid package. The docs are pretty straightforward, so according to the readme file a UUID should be generated just by calling $uuid = Uuid::generate();, but it returns an empty object. (I also tried $uuid = Uuid::generate(1);)

I followed the installation instructions as provided there (nothing out of the ordinary), the app doesn't throw any errors, so I guess everything is right.

这方面的其他方案也受欢迎.

推荐答案

After laravel 5.6 a new helper was added to generate Universal Unique Identifiers (UUID)

use Illuminate\Support\Str;

return (string) Str::uuid();

return (string) Str::orderedUuid();

这些方法返回一个Ramsey\Uuid\Uuid对象

orderedUuid()方法将首先生成一个时间戳UUID,以便更容易、更高效地建立数据库索引.

Laravel相关问答推荐

如何使用 Laravel 进行继承

Laravel 10 - 没有 $append 属性的Eloquent 的热切加载

不能在 laravel 中使用控制器名称,必须使用命名空间

如何使用 Laravel 查询生成器在 WHERE 条件周围添加括号

laravel Eloquent 模型更新事件未触发

Laravel Eloquent,仅 Select 存在关系的行

Laravel 5.1 - 如何在进度条上设置消息

错误try 访问 null 类型值的数组偏移量laravel 5.8.26 Php 7.4.2

Laravel - 验证 - 如果字段为空则需要

Laravel whereDoesntHave() - 多个 OR 条件

不支持驱动Driver[] - Laravel 5.3

Laravel lockforupdate(悲观锁)

在 laravel 分页中添加一些数据

Laravel 同步错误

Laravel 在特定文件夹中创建迁移文件

如何在 Laravel 5 中按键获取所有缓存项目的列表?

使用 Laravel 将两个模型合并到一个分页查询中,并带有Eager加载的关系

找不到框laravel/homestead

在 Laravel 中结合 AND/OR Eloquent的查询

遍历 Laravel 控制器中的结果集