Laravel - 用户 table,将 id 设置为 uuid 类型
Laravel - User table, make id uuid type
1- 用户table,使id uuid类型.
没问题
php artisan migrate:refresh
但是这个错误
php artisan db:seed
错误:("SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value")
2-还有公司希望随机分发给用户。在用户table中,uuid类型会被保存在user_id列。
从现在开始谢谢你...
用户模型:
use UsesUuid;
protected $fillable = ['name', 'email', 'password', 'role', 'slug',];
protected $hidden = ['password', 'remember_token',];
protected $casts = ['email_verified_at' => 'datetime',];
public function companies()
{
return $this->hasMany('App\Company', 'user_id', 'id');
}
UsesUuid 特征:
protected static function boot()
{
parent::boot();
static::creating(function ($post) {
$post->{$post->getKeyName()} = (string)Str::uuid();
});
}
public $incrementing = false;
public function getKeyType()
{
return 'string';
}
用户迁移:
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary()->unique();
$table->string('name',100);
$table->string('email',100);
$table->timestamp('email_verified_at')->nullable();
$table->string('password',100);
$table->string('role',20)->nullable();
$table->string('slug',100);
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
公司迁移:
Schema::create('companies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('user_id',36);
$table->string('name', 100);
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
用户工厂:
$name = $faker->name;
return [
'id' => Str::uuid(),
'name' => $name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => Hash::make(123), // password
'remember_token' => Str::random(10),
'role' => 'user',
'slug' => Str::slug($name),
];
公司工厂:
$name = $faker->company;
return [
'user_id' => Str::uuid(),
'name' => $name,
];
数据库播种器:
factory(App\User::class, 5)->create();
factory(App\Company::class, 1500)->create();
像这样修改你的特质:
static::creating(function ($post) {
empty($post->{$post->getKeyName()}) && $post->{$post->getKeyName()} = (string)Str::uuid();
});
没有必要让UUID唯一,
在您的迁移中,因为它已经存在!
$table->uuid('id');
$table->primary('id');
并且工厂必须自己创建主 UUID,不要自己添加
我认为有了这些变化,播种机必须 运行 成功
1- 用户table,使id uuid类型.
没问题
php artisan migrate:refresh
但是这个错误
php artisan db:seed
错误:("SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value")
2-还有公司希望随机分发给用户。在用户table中,uuid类型会被保存在user_id列。
从现在开始谢谢你...
用户模型:
use UsesUuid;
protected $fillable = ['name', 'email', 'password', 'role', 'slug',];
protected $hidden = ['password', 'remember_token',];
protected $casts = ['email_verified_at' => 'datetime',];
public function companies()
{
return $this->hasMany('App\Company', 'user_id', 'id');
}
UsesUuid 特征:
protected static function boot()
{
parent::boot();
static::creating(function ($post) {
$post->{$post->getKeyName()} = (string)Str::uuid();
});
}
public $incrementing = false;
public function getKeyType()
{
return 'string';
}
用户迁移:
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary()->unique();
$table->string('name',100);
$table->string('email',100);
$table->timestamp('email_verified_at')->nullable();
$table->string('password',100);
$table->string('role',20)->nullable();
$table->string('slug',100);
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
公司迁移:
Schema::create('companies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('user_id',36);
$table->string('name', 100);
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
用户工厂:
$name = $faker->name;
return [
'id' => Str::uuid(),
'name' => $name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => Hash::make(123), // password
'remember_token' => Str::random(10),
'role' => 'user',
'slug' => Str::slug($name),
];
公司工厂:
$name = $faker->company;
return [
'user_id' => Str::uuid(),
'name' => $name,
];
数据库播种器:
factory(App\User::class, 5)->create();
factory(App\Company::class, 1500)->create();
像这样修改你的特质:
static::creating(function ($post) {
empty($post->{$post->getKeyName()}) && $post->{$post->getKeyName()} = (string)Str::uuid();
});
没有必要让UUID唯一, 在您的迁移中,因为它已经存在!
$table->uuid('id');
$table->primary('id');
并且工厂必须自己创建主 UUID,不要自己添加
我认为有了这些变化,播种机必须 运行 成功