gpt4 book ai didi

php - 动态调查应用程序数据库结构

转载 作者:行者123 更新时间:2023-11-29 19:28:19 25 4
gpt4 key购买 nike

我正在创建一个用于创建调查的应用程序。但我在数据库结构方面遇到了困难。

My Tables:
Surveys:
- id;
- user_id;
- title;
- status;

Questions:
- id;
- survey_id;
- label;
- input_type;
- options;
- order;

我唯一的问题是我需要能够创建某种级联或填充功能。

例如:假设我有一个问题(单选按钮),答案有 2 个选项,“”或“” ”,但如果用户选择"is",则会出现另一个问题。

我如何构建我的表格?

最佳答案

要具有链接功能,您可以在问题表中使用自关系。在您的迁移中添加此内容。

$table->integer('questions_id')->unsigned()->nullable();
$table->foreign('questions_id')->references('id')->on('questions');

然后在您的模型中您可以执行以下操作。

use Illuminate\Database\Eloquent\Model;

class Question extends Model
{
public function children()
{
return $this->belongsTo(Question::class, 'questions_id');
}
}

然后,如果您选择"is",请传递问题 ID,您就可以像这样查询。

$questions = Question::with('children')->find($questionId);
// $questions has related questions

您可以使用此方法作为基础并对其进行改进。

编辑

根据您的评论,您需要一个包含 answer_idquestion_idanswer_question 数据透视表。

在答案模型中 使用 Illuminate\Database\Eloquent\Model;

class Answer extends Model
{
public function questions()
{
return $this->belongsToMany(Question::class, 'answer_question');
}
}

当你查询时

$answerWithQuestions = Answers::with('questions')->find($answerId);
// $answerWithQuestions has related Questions

关于php - 动态调查应用程序数据库结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41985148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com