如何将表单 ID 和用户 ID 分配给另一个 table
How to assign form ID and user ID to another table
我有这个 table 来创建一个新用户:
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('student_id');
$table->string('student_faculity');
$table->string('email')->unique();
$table->string('usertype')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->string('profile_photo_path', 2048)->nullable();
$table->timestamps();
});
}
和这个 table 创建一个新表单:
{
Schema::create('files', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('file_path')->nullable();
$table->string('name_as_id')->nullable();
$table->string('email')->nullable();
$table->string('position')->nullable();
$table->text('bio')->nullable();
$table->string('approve')->nullable();
$table->string('votes')->default('0');
$table->string('image')->nullable();
$table->timestamps();
});
}
我正在尝试创建第三个 table 来处理每个用户的投票(每个用户可以为每个表单投票一次)
这里的外键怎么处理?
我试着像这样创建第三个 table:
{
Schema::create('voted', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('file_id');
$table->string('voted?')->default('No');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('file_id')->references('id')->on('files')->onDelete('cascade');
});
}
但是当用户创建或上传表单时我没有得到任何数据
您可以在这里找到类似的表结构:Whosebug
在迁移下,在模型中 class 您可以找到关系。
我有这个 table 来创建一个新用户:
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('student_id');
$table->string('student_faculity');
$table->string('email')->unique();
$table->string('usertype')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->string('profile_photo_path', 2048)->nullable();
$table->timestamps();
});
}
和这个 table 创建一个新表单:
{
Schema::create('files', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('file_path')->nullable();
$table->string('name_as_id')->nullable();
$table->string('email')->nullable();
$table->string('position')->nullable();
$table->text('bio')->nullable();
$table->string('approve')->nullable();
$table->string('votes')->default('0');
$table->string('image')->nullable();
$table->timestamps();
});
}
我正在尝试创建第三个 table 来处理每个用户的投票(每个用户可以为每个表单投票一次)
这里的外键怎么处理?
我试着像这样创建第三个 table:
{
Schema::create('voted', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('file_id');
$table->string('voted?')->default('No');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('file_id')->references('id')->on('files')->onDelete('cascade');
});
}
但是当用户创建或上传表单时我没有得到任何数据
您可以在这里找到类似的表结构:Whosebug
在迁移下,在模型中 class 您可以找到关系。