Php laravel获得阅读理解多项选择题和单个mcq';他参加了一次考试

Php laravel获得阅读理解多项选择题和单个mcq';他参加了一次考试,php,laravel,Php,Laravel,我有两种要求 1) 具有直接问题和多项选择答案(单选答案)的测验 2) 阅读理解测验,包括一组问题和一些分类问题,然后再复习课文。(段落和随机问题没有特定的出现顺序) 我通过构建下表实现了任务1 Table( questions) $table->integer('quiz_id')->index(); $table->integer('question_no')->index(); $table->text('que

我有两种要求

1) 具有直接问题和多项选择答案(单选答案)的测验

2) 阅读理解测验,包括一组问题和一些分类问题,然后再复习课文。(段落和随机问题没有特定的出现顺序)

我通过构建下表实现了任务1

Table( questions)
        $table->integer('quiz_id')->index();
        $table->integer('question_no')->index();
        $table->text('question');
        $table->text('quiz_ans_1');
        $table->text('quiz_ans_2');
        $table->text('quiz_ans_3');
        $table->text('quiz_ans_4');

Table (answers)
        $table->increments('id');
        $table->integer('question_id');
        $table->integer('quiz_id')->index();
        $table->integer('question_no')->index();
        $table->string('answer');
对于普通的直接测试,它非常有效,因为在blade模板中,我可以提取问题标题和答案并运行“foreach”循环

对于任务2,我不知道应该将阅读理解文章放在数据库中的什么地方,然后按照考试格式(考试格式各不相同)将它们放在刀刃上

几个问题:-

1) 如果创建了带有hasMany('App\Questions')的模型“passions”。然后,当我叫一个测验,拉动刀锋,它将只显示与之相关的段落和问题,并忽略。但仍有许多问题需要解决

2) 如果创建了属于某一段落的“问题”模型,则定义每个问题都属于某一段落,而事实并非如此

我想在在线测试的一个实例中实现以下内容:-

1) 第1节->“n”独立问题的数量

2) 第2节->三篇阅读理解文章,并附有自己的一组问题(数字各不相同)

3) 第3节->独立的“m”问题

4) 第4节->两篇阅读理解文章,并附有自己的问题集(数字不同)

总问题数(n+m+l)=100(或固定的200)


如果有人能把我引向正确的方向,那将非常有帮助。

你需要让你的问题具有多态性。这意味着它们可能属于测试(独立问题)、段落(段落问题)或测验(测验问题)。在这里阅读多态关系

问题表

id - integer
questionable_id - integer
questionable_type - string
id - integer
question_id - integer
content - text
因为您的问题现在是多焦点的,所以您不能再进行
测验1
测验2
等。所以你为他们做了一张桌子

选项表

id - integer
questionable_id - integer
questionable_type - string
id - integer
question_id - integer
content - text
然后你有你的模型测试(或考试),文章,测验,问题,选项,答案。在这些模型中,向问题模型添加关系

public function questions() {
    return $this->morphMany(Question::class, 'questionable');
}
有问题的模型添加

public function questionable() {
    return $this->morphTo();
}
顺便说一句,在表格答案中,删除
quick\u id
<代码>问题id足以知道它属于什么。此外,你可以回答任何问题,而不仅仅是测验问题

现在,在你的考试/测试中,你可以得到这样的答案

//get the exam first
$exam = Exam::with('questions')->find($examid);

//$exam hasMany [quizz, passages, questions] (independant questions)

//quiz questions
$quiz = Quiz::with('questions')->where('exam_id', $exam->id)->first();

//passage questions
$passages = Passage::with('questions')->where('exam_id', $exam->id)->get();

//independant questions
$questions = $exam->questions; 
//1. Section 1, 10 independent questions
@php $firstTenQuestions = $questions->take(10); @endphp
@foreach($firstTenQuestions->all() as $question)
    {{ $question->content }}
@endforeach

//2. Reading passages (3)
@php $firstThreeReadings = $readings->take(3); @endphp
@foreach($firstThreeReadings->all() as $reading)
    {{ $reading->content }}

    @foreach($reading->questions as $question)
        {{ $question->content }}
    @endforeach
@endforeach

//3. Second set of independent questions 
// get questions from index 10 [11 - n]
@php $secondSet = $questions->splice(10); @endphp 
@foreach($secondSet->all() as $question)
    {{ $question->content }}
@endforeach

//4. Reading Passages (2)
// take from index 3, limit it to 2
@php $readings2 = $readings->splice(3, 2); @endphp
@foreach($readings2->all() as $reading)
    {{ $reading->content }}

    @foreach($reading->questions as $question)
        {{ $question->content }}
    @endforeach
@endforeach
在您的视图中,可以这样显示

//get the exam first
$exam = Exam::with('questions')->find($examid);

//$exam hasMany [quizz, passages, questions] (independant questions)

//quiz questions
$quiz = Quiz::with('questions')->where('exam_id', $exam->id)->first();

//passage questions
$passages = Passage::with('questions')->where('exam_id', $exam->id)->get();

//independant questions
$questions = $exam->questions; 
//1. Section 1, 10 independent questions
@php $firstTenQuestions = $questions->take(10); @endphp
@foreach($firstTenQuestions->all() as $question)
    {{ $question->content }}
@endforeach

//2. Reading passages (3)
@php $firstThreeReadings = $readings->take(3); @endphp
@foreach($firstThreeReadings->all() as $reading)
    {{ $reading->content }}

    @foreach($reading->questions as $question)
        {{ $question->content }}
    @endforeach
@endforeach

//3. Second set of independent questions 
// get questions from index 10 [11 - n]
@php $secondSet = $questions->splice(10); @endphp 
@foreach($secondSet->all() as $question)
    {{ $question->content }}
@endforeach

//4. Reading Passages (2)
// take from index 3, limit it to 2
@php $readings2 = $readings->splice(3, 2); @endphp
@foreach($readings2->all() as $reading)
    {{ $reading->content }}

    @foreach($reading->questions as $question)
        {{ $question->content }}
    @endforeach
@endforeach

这不是解决办法。但我相信你可以从中得到你想要的。在这里学习收集操作

谢谢你的回答。这是一个很好的方法,但只解决了一半的问题。问题是我们不知道第一部分是否包含10个问题。有些时候,他们可以有5个、4个或15个。文章和它的问题以及其他问题也是一样的。它们在不同的测试中有所不同。如何标记它们独立和与文章相关,并在每次考试中使用它们。我想标准化的模板,而不是有100的刀片模板。唯一固定的是问题总数。在考试/测试表中,将问题数添加到一列中,如
section1\u questions integer NOT NULL
。然后在第一个循环中执行
$firstTenQuestions=$questions->take($exam->section1_-questions)。你可以把同样的东西用在段落上。这样,就需要一个单一的模板。我已经做了一些修改,并按照你的建议建立了它。它工作起来很有魅力!谢谢。被接受为答案。