电子邮件作为拉雷夫(Larave)的外键

发布于 2025-02-04 11:47:58 字数 537 浏览 3 评论 0原文

我可以将电子邮件作为外键吗? $ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷血 2025-02-11 11:47:58

要与电子邮件 用户表创建外键关联,您不能使用foreferid方法,因为它创建 unsignedBiginteger 等效列。

您可以创建外国密钥关联,

//Some other table - for eg lets say posts
Schema::create('posts', function(Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('body');
    $table->string('author_email');

    $table->foreign('author_email') // a column on posts table
        ->references('email') //name of the column on users (referenced) table
        ->on('users')    //name of the referenced table 
        ->onDelete('cascade'); //constrain
});

然后使用此外国密钥协会,您可以在 post 模型中定义作者关系,将其链接到 user 模型

//Post.php - eloquent model class
public function author()
{
    return $this->belongsTo(User::class, 'author_email', 'email');
}

注:为此,它可以作为工作预期,电子邮件 用户表必须包含唯一值,即具有 unique 索引(如用户表的迁移中所示)

laravel docs-迁移 - 迁移 - 外键约束

To create an Foreign Key association with email column of users table, you cannot use foreignId method as it creates unsignedBigInteger equivalent column.

You can create the foreign key association as

//Some other table - for eg lets say posts
Schema::create('posts', function(Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('body');
    $table->string('author_email');

    $table->foreign('author_email') // a column on posts table
        ->references('email') //name of the column on users (referenced) table
        ->on('users')    //name of the referenced table 
        ->onDelete('cascade'); //constrain
});

Then using this foreign key association you can define the author relation in Post model linking it to the User model

//Post.php - eloquent model class
public function author()
{
    return $this->belongsTo(User::class, 'author_email', 'email');
}

Note: For this to work as expected, email column on users 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文