astrotomic / laravel-eloquent-uuid
1.5.0
2022-04-12 10:54 UTC
Requires
- php: ^7.4 || ^8.0
- illuminate/database: ^8.0 || ^9.0
- ramsey/uuid: ^3.7 || ^4.0
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ^6.0 || ^7.0
- phpunit/phpunit: ^9.3
README
为您的 Eloquent 模型提供简单的 UUID 支持解决方案。
安装
您可以通过 composer 安装此包。
composer require astrotomic/laravel-eloquent-uuid
使用方法
您可以使用提供的 UsesUUID
特性添加一个 UUID 属性,作为您的常规主键或将其作为主键使用。
模型
这将添加一个 uuid
属性,并在模型创建之前自动填充。
use Illuminate\Database\Eloquent\Model; use App\Models\Concerns\UsesUUID; class Post extends Model { use UsesUUID; }
如果您想自定义属性名,可以定义一个 $uuidName
属性或覆盖 getUuidName()
方法。
use Illuminate\Database\Eloquent\Model; use App\Models\Concerns\UsesUUID; class Post extends Model { use UsesUUID; protected $uuidName = 'token'; }
当然,您也可以使用这个特性来定义模型的主键。
use Illuminate\Database\Eloquent\Model; use App\Models\Concerns\UsesUUID; class Post extends Model { use UsesUUID; public $incrementing = false; protected $keyType = 'string'; public function getUuidName(): string { return $this->getKeyName(); } }
迁移
Laravel 在 Blueprint
类的表上提供了一个 uuid()
列类型。
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; Schema::create('posts', function (Blueprint $table) { // ... $table->uuid('uuid')->unique(); // ... });
测试
composer test
变更日志
请参阅 CHANGELOG 以获取有关最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 以获取详细信息。您也可能对 CODE OF CONDUCT 感兴趣。
安全
如果您发现任何与安全相关的问题,请检查 SECURITY 了解报告步骤。
致谢
替代方案
许可
The MIT License (MIT)。请参阅 许可文件 以获取更多信息。
实物货
您可以使用此包,但如果它进入您的生产环境,我将非常感激您为世界购买一棵树。
现在众所周知,应对气候危机并防止气温上升超过1.5摄氏度最好的工具之一就是植树。如果您为我的森林做出贡献,您将为当地家庭创造就业机会并恢复野生动物栖息地。
您可以在offset.earth/treeware购买树木。
了解更多关于Treeware的信息,请访问treeware.earth