kduma / eloquent-uuidable
简化了在Laravel Eloquent模型中使用和生成uuid的过程。
v1.0.1
2020-05-06 12:02 UTC
Requires
- php: >=5.4.0
- ramsey/uuid: ^3.8|^3.9|^4.0
This package is auto-updated.
Last update: 2024-08-29 18:19:10 UTC
README
简化了在Laravel Eloquent模型中使用和生成guid的过程。
设置
使用composer安装它
composer require kduma/eloquent-uuidable
准备模型
在模型内部(不要在文件顶部)添加以下行
use \KDuma\Eloquent\Uuidable;
在数据库中创建uuid
字符串字段。如果你使用迁移,可以使用以下代码片段
$table->uuid('uuid')->unique();
用法
默认情况下,在首次保存时生成短链接。
$model->regenerateUuid()
- 生成新的uuid。 (记得自己保存它)Model::whereUuid($uuid)->first()
- 通过uuid查找。 (whereUuid
是查询作用域)
从kduma/eloquent-guidable
的1.x/2.x版本升级
将以下行添加到你的模型中,以将使用uuid
列名切换到guid
,就像之前版本中使用的那样
protected $uuid_field = 'guid';
Packagist
在Packagist.org上查看此包:kduma/eloquent-uuidable