waska14 / laravel-uuid
在模型中轻松使用uuid
Requires
- php: >=5.5.9
This package is auto-updated.
Last update: 2024-09-20 18:39:01 UTC
README
如果你想要使用uuid列并自动填充它们,这个包就是为你准备的。
这是一个Laravel包,用于在模型中使用(自动生成)uuid。使用这个包,你可以写更少的代码,因为uuid在第一次保存实例时就会创建。
文档
Laravel兼容性
安装
通过执行命令将包添加到你的composer.json中。
composer require waska14/laravel-uuid
接下来,在你的config/app.php
中添加服务提供者
'providers' => [
....
Waska\LaravelUuid\UuidServiceProvider::class,
....
],
迁移
// 2.1 If you want to use uuid column as primary column:
Schema::create('students', function(Blueprint $table)
{
$table->uuid('uuid')->primary();
$table->string('name');
$table->string('last_name');
...
});
// 2.2 If you want to use uuid column as non-primary column:
Schema::create('students', function(Blueprint $table)
{
$table->increments('id');
$table->uuid('uuid')->unique();
$table->string('name');
$table->string('last_name');
...
});
// 2.3 If you want to use multiple uuid columns (maybe on of them is primary):
Schema::create('students', function(Blueprint $table)
{
$table->uuid('uuid')->primary();
$table->uuid('uuid_column_2')->unique();
$table->uuid('uuid_column_3')->unique();
$table->string('name');
$table->string('last_name');
...
});
注意:如果uuid列不是主键,你应该使其唯一,或者手动索引该列。
模型
- 必须在模型中使用特质
class Student extends Model
{
use \Waska\Traits\Uuid;
}
2.1 如果你使用uuid列作为主键且column_name 默认 id
,你不需要做更多。
2.2 如果你使用uuid列作为主键且column_name 不是默认 id
,你必须定义protected $primaryKey
和protected $uuid_column
class Student extends Model
{
use \Waska\Traits\Uuid;
protected $primaryKey = "uuid_primary_column_name";
protected $uuid_column = "uuid_primary_column_name";
protected $fillable = [
'name',
'last_name',
];
}
2.3 如果你使用非主键uuid列且列名等于default_column_name
(来自配置的uuid),你只需在protected $fillable
中添加列名。
class Student extends Model
{
use \Waska\Traits\Uuid;
protected $fillable = [
'name',
'last_name',
'uuid', // config/waska.uuid.php -> default_column_name
];
}
2.4 如果你使用非主键uuid列且列名不等于default_column_name
(来自配置的uuid),你需要定义protected $uuid_column
并在protected $fillable
中添加列名。
class Student extends Model
{
use \Waska\Traits\Uuid;
protected $uuid_column = "uuid_column_name";
protected $fillable = [
'name',
'last_name',
'uuid_column_name',
];
}
2.5 如果你使用多个uuid列(如果其中一个是主键,你必须首先执行步骤2.1),你需要将protected $uuid_column
定义为一个数组,并在protected $fillable
中添加列名(仅非主键)。
class Student extends Model
{
use \Waska\Traits\Uuid;
protected $primaryKey = "uuid_primary_column_name"; // If one of them is primary
protected $uuid_column = ["uuid_column_name1", "uuid_column_name2", "uuid_column_name3"];
protected $fillable = [
'name',
'last_name',
'uuid_column_name1',
'uuid_column_name2',
'uuid_column_name3',
];
}
2.6 现在当你创建新的Student
对象时,uuid会自动生成。
Student::create([
'name' => "Bill',
'last_name' => "Gates',
]);
// or
$student = new Student([
'name' => "Bill',
'last_name' => "Gates',
]);
$student->save();
// or
$student = new Student();
$student->name = "Bill";
$student->last_name = "Gates";
$student->save();
配置
如果你想更改默认配置,你必须将默认配置文件发布到你的项目中。
你可以在控制台中运行此命令来发布配置
php artisan vendor:publish --tag=waska-uuid-config
注意:如果你打算使用v3
或v5
uuid,建议将v3_default_namespace
和v5_default_namespace
更改为有效的uuid字符串。
使用tinker中的此命令生成它们(启动tinker:php artisan tinker
)
Waska\Uuid::get(4); // This command will generate valid uuid (v4, pseudo-random) string
其他使用方法
生成uuid(通用唯一标识符)
/**
* This generates name-based Uuid
* @param int $version.
* @param string $name. String which the uuid is generating for.
* @param string $namespace. Valid uuid string. Default value is defined in config/waska.uuid.php
* @return String
*/
Waska\Uuid::get(3, "some_random_string", "valid_uuid");
// This generates pseudo-random Uuid
Waska\Uuid::get(); // Default version is 4, so it means: Waska\Uuid::get(4);
/**
* This generates name-based Uuid
* @param int $version.
* @param string $name. String which the uuid is generating for.
* @param string $namespace. Valid uuid string. Default value is defined in config/waska.uuid.php
* @return String
*/
Waska\Uuid::get(5, "some_random_string", "valid_uuid");