laravel在单一考试中获得阅读理解多项选择题和单一mcq

I have two types of requirements.

1) Quiz with straight forward questions and multiple choice answer (single answer)

2) Quiz with reading comprehension with its set of questions and some assorted questions then passage again. (There is no particular order in which passages and random questions appear)

I have implemented task 1 by building the following tables.

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');

For normal straight forward quiz it works great as in blade template I can pull a question title and answers and run a "foreach" loop.

For task 2 ,I am confused where to put the reading comprehension passages in database and pull them in blade as described in test format which vary from exam to exam.

Few problems :-

1) If model "passages" with hasMany('App\Questions') is created. Then when I call a quiz and pull blade then it will show only passages and questions related to it and ignore . But there will be many single questions which needs to be pulled.

2) If model "questions" belongsTo Passage is created it is defining that every question belongs to some passage which is not the case.

I want to implement the following in a instance of online test :-

1) section 1 -> "n" number of questions which are independent

2) section 2 -> three reading comprehension passages with their own set of questions (numbers vary)

3) section 3 -> "m" questions which are independent

4) section 4 -> two reading comprehension passages with their own set of questions (numbers vary)

total questions (n+m+l)= 100 (or 200 which is fixed)

It would be great help if some can put me in right direction.

You need to make your questions polymorphic. Meaning they could belong to the test (independent questions), to a passage (passage questions) or to a quiz (quiz questions). Read about polymorphic relationships here https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations

Questions table

id - integer
questionable_id - integer
questionable_type - string

Because your questions are polyphormic now, you cannot have quiz_ans_1, quiz_ans_2, etc. anymore. So you make a table for them

Options table

id - integer
question_id - integer
content - text

Then you have your models Test (or Exam), Passage, Quizz, Question, Option, Answer. In these models, add a relationship to Question model

public function questions() {
    return $this->morphMany(Question::class, 'questionable');
}

in Question model add

public function questionable() {
    return $this->morphTo();
}

Btw, in table answers, remove quiz_id. question_id is enough to know what it belongs to. Besides, you can answer to any question, not only quiz questions.

Now in your exam/test you can get them like this

//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; 

In your view, you can display like this

//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

This is not THE solution. But I'm sure you can build from this to get where you want. Learn about collection manipulations here https://laravel.com/docs/5.4/collections