Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 动态测量应用程序数据库结构_Php_Mysql_Laravel_Laravel 5_Laravel 5.3 - Fatal编程技术网

Php 动态测量应用程序数据库结构

Php 动态测量应用程序数据库结构,php,mysql,laravel,laravel-5,laravel-5.3,Php,Mysql,Laravel,Laravel 5,Laravel 5.3,我正在创建一个用于创建调查的应用程序。但我在数据库结构中苦苦挣扎 My Tables: Surveys: - id; - user_id; - title; - status; Questions: - id; - survey_id; - label; - input_type; - options; - order; 我唯一的问题是,我需要能够创建某种级联或填充特性 例如:假设我有一个问题(单选按钮),其中答案有两个选项,yes”或“no”,但如果用户选择“yes”,例如,它会出现另一个

我正在创建一个用于创建调查的应用程序。但我在数据库结构中苦苦挣扎

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

Questions:
- id;
- survey_id;
- label;
- input_type;
- options;
- order;
我唯一的问题是,我需要能够创建某种级联或填充特性

例如:假设我有一个问题(单选按钮),其中答案有两个选项,
yes
”或“
no
”,但如果用户选择“yes”,例如,它会出现另一个问题


如何构建表格?

要拥有链接功能,您可以在问题表格中使用自我关系。在您的迁移中添加这个

$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');
    }
}
然后,如果选择“是”,则传递
问题id
,您可以这样查询

$questions = Question::with('children')->find($questionId);
// $questions has related questions
您可以将此方法作为基础并加以改进

编辑

根据您的评论,您需要一个带有
answer\u id
question\u id
pivot表

应答模型 使用Illumb\Database\Elount\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

你好,Gayan,谢谢你的反馈,但是我将如何根据用户的回答将新问题联系起来?例如,我有4个可用的输入复选框类型,用户选择第二个复选框的答案,根据该答案给出另一个问题,我会构建这种情况吗?我想我还需要一张桌子来建立这种关系。