mykemeynell / laraveluuid
本软件包最新版本(1.0.3)无可用许可证信息。
Laravel项目的UUID列功能
1.0.3
2020-07-23 18:06 UTC
Requires
- php: >=7.1.0
- ramsey/uuid: ^4.0
This package is auto-updated.
Last update: 2024-09-24 03:08:10 UTC
README
轻松开始在您的Laravel应用程序中使用UUID,而不是默认的自增ID。
使用Composer安装
composer require mykemeynell/laraveluuid
用法
- 将UUID列添加到迁移文件中,例如默认的“创建用户”迁移。
use ...; class CreateUsersTable extends Migration { // Include the UuidColumn trait into your migration class. // This will give you access to the necessary method to easily // create the appropriate column. use \UuidColumn\Concern\UuidColumn; public function up() { Schema::create('users', function(Blueprint $table) { // Calling the createUuidColumn() method will add the column into your migration. // You can migrate this normally with PHP Artisan. $this->createUuidColumn($table, 'id); ...
- 将
HasUUidObserver特性添加到您的Model中。例如,在默认的User模型app/User.php中。
use ...; use UuidColumn\Concern\HasUuidObserver; class User extends Authenticatable { use ..., HasUuidObserver; ...
- 您需要更改模型中的默认值以反映新的键类型和行为
class User extends Authenticatable { ... /** * Indicates if the IDs are auto-incrementing. * * @var bool */ public $incrementing = false; /** * The "type" of the primary key ID. * * @var string */ protected $keyType = 'string'; ...
就这样!当创建新记录时,将在ID处设置新的UUID4字符串。