Laravel-用户表,生成id uuid类型

Laravel-用户表,生成id uuid类型,laravel,laravel-5,factory,uuid,laravel-5.8,Laravel,Laravel 5,Factory,Uuid,Laravel 5.8,1-用户表,生成id uuid类型。 没问题 但是这个错误 错误:(“SQLSTATE[HY000]:一般错误:1364字段“id”没有默认值”) 2-公司也希望随机分配给用户。在用户表中,uuid类型将保存在用户id列中。 从现在起感谢您…… 用户模型: UseSuid特征: 用户迁移: 公司迁移: })) 用户工厂: 公司工厂: $name = $faker->company; return [ 'user_id' => Str::uuid(), 'name' =

1-用户表,生成id uuid类型。

没问题

但是这个错误

错误:(“SQLSTATE[HY000]:一般错误:1364字段“id”没有默认值”)

2-公司也希望随机分配给用户。在用户表中,uuid类型将保存在用户id列中。

从现在起感谢您……

用户模型:

UseSuid特征:

用户迁移:

公司迁移:

}))

用户工厂:

公司工厂:

$name = $faker->company;
return [
    'user_id' => Str::uuid(),
    'name' => $name,
];
数据库播种机:


修改你的特征如下:

static::creating(function ($post) {
    empty($post->{$post->getKeyName()}) && $post->{$post->getKeyName()} = (string)Str::uuid();
});
无需使UUID唯一, 在您的迁移中,因为它已经存在

$table->uuid('id');
$table->primary('id');
工厂必须自己创建主UUID,不要自己添加


我认为有了这些更改,seeder必须成功运行

一般错误:1364字段“id”没有默认值”)
,将
id
列设为自动增量或从代码中提供值。我该怎么办?错误:SQLSTATE[HY000]:一般错误:1005无法创建表
bulursun.com
公司
(错误号:150“外键约束的格式不正确”)(SQL:alter table
COMPANYS
add constraint
COMPANYS\u user\u id\u EXFOREIGN
外键(
user\u id
)引用
users
id
)删除级联)在company factory中,不要创建新的UUID,只需随机选择一个用户id,因为我们对该字段有一个约束“user\u id”=>user::where('deleted\u at',null)->inRandomOrder()->first()->id;在迁移中最好使用:$table->UUID('user\id);而不是36个字符的字符串
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();
});
$table->uuid('id');
$table->primary('id');