电子邮件作为拉雷夫(Larave)的外键
我可以将电子邮件作为外键吗? $ table-> foreferid('email') - >限制('用户') - > ondelete('cascade');
。我写了一个播种机,是为Emai的整数价值。我需要使电子邮件唯一,
这是我的用户表
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Can I make email as a foreign key ? $table->foreignId('email')->constrained('users')->onDelete('CASCADE');
. I wrote a seeder and is for an integer value for emai. I need to make email unique
This is my users table
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要与
电子邮件
用户
表创建外键关联,您不能使用foreferid
方法,因为它创建 unsignedBiginteger 等效列。您可以创建外国密钥关联,
然后使用此外国密钥协会,您可以在 post 模型中定义作者关系,将其链接到 user 模型
注:为此,它可以作为工作预期,
电子邮件
用户
表必须包含唯一值,即具有 unique 索引(如用户表的迁移中所示)laravel docs-迁移 - 迁移 - 外键约束
To create an Foreign Key association with
email
column ofusers
table, you cannot useforeignId
method as it creates unsignedBigInteger equivalent column.You can create the foreign key association as
Then using this foreign key association you can define the author relation in Post model linking it to the User model
Note: For this to work as expected,
email
column onusers
table must contain unique values i.e have a unique index (as you have in the migration of users table)Laravel Docs - Migrations - Foreign Key Constraints