首页 > 解决方案 > Laravel 8 - 用户表中的两个外键

问题描述

我想创建两个人可以创建会议室的应用程序。但我不知道如何在 Laravel 中制作模式。

我试图这样创建:

Schema::create('appointments_tables', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('user_id2');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 
            $table->foreign('user_id2')->references('id')->on('users')->onDelete('cascade');      
            $table->datetime('start');
    });
}

这是正确的方法吗?

标签: phpmysqllaraveldatabase-designlaravel-8

解决方案


是的,这是正确的。

您还可以使用工匠命令“php artisan make:migration”。

以下是该命令帮助标志的详细信息:

$ php artisan -help 制作:迁移

描述:

创建一个新的迁移文件

用法:

make:migration [选项] [--]

论据:

name 迁移的名称

选项:

  --create[=CREATE]  The table to be created
  --table[=TABLE]    The table to migrate
  --path[=PATH]      The location where the migration file should be created
  --realpath         Indicate any provided migration file paths are pre-resolved absolute paths
  --fullpath         Output the full path of the migration

-h, --help 显示此帮助消息 -q, --quiet 不输出任何消息 -V, --version 显示此应用程序版本 --ansi 强制 ANSI 输出 --no-ansi 禁用 ANSI 输出 -n, -- no-interaction 不要问任何交互式问题 --env[=ENV] 命令应该运行的环境 -v|vv|vvv, --verbose 增加消息的详细程度:1 表示正常输出,2 表示更详细的输出和3 用于调试


推荐阅读