lascoff
lascoff

Reputation: 1331

Laravel 4 - nested results

I am new to php and larval. I have the following database tables questions and answers. Each question can have multiple answers with the corrected one indicated in the correct answer field. I am trying to list out the question with the answers following like this

this is the question.... a. first answer b. second answer c. third answer d. fourth answer

I have the following code:

public static function getMultipleChoiceQuestions($chapter)
{
    $data = DB::table('newQuestions')
        ->join('newAnswers', 'newQuestions.questionId', '=', 'newAnswers.questionId')
        ->where('chapterId', '=', $chapter)->orderBy('newQuestions.questionId')
        ->where('questionType', '=', "1")
        ->get();

    $questions = array('questionType' => $questionType, 'data' => $data);
    return $questions;
}

question table: chapterId questionId questionText

answer table: answerId questionId answerText correctAnswer

The following code displays the question for each answer.

<fieldset id="group_1">
    <p><div><input type="checkbox" class="checkall"> Check all</div></p>
    <div style="width:600px; height:300px; overflow: auto;">
        @foreach($questions['data'] as $question)
            <p><input name="your_name" value="{{ $question->questionId }}" id="{{ $question->questionId }}" type="checkbox" class="questionsOnPage" />
                {{ $question->questionText }}</p>
        @endforeach
    </div>
</fieldset>

I would like to list question then answers then next question.

Please help!

Upvotes: 0

Views: 158

Answers (1)

Sajan Parikh
Sajan Parikh

Reputation: 4960

I think you need to take a further look at Eloquent. You should be able to do something like this.

Here is how you would set up the relationship. Based on this, your answers table name needs to be answers, and it needs a column named question_id. Although, you can go through the docs to learn how to set custom table names and column names.

app/models/Question.php

class Question extends Eloquent {

    public function answers()
    {
        return $this->hasMany('Answer');
    }
}

app/models/Answer.php

class Answer extends Eloquent {

    public function question()
    {
        return $this->belongsTo('Question')
    }
}

Now, once those relationships are set like that, we can really use Eloquent. You can do something like this in your view quite easily with blade.

The outer foreach will loop through each question. The the inner foreach will display each answer that belongs to that current question, then move on to the next question.

@foreach(Question::all() as $question)
    <h3>{{ $question->title }}</h3>
    <ul>
    @foreach($question->answers->all() as $answer)
        <li>{{$answer->text}}</li>
    @endforeach
    </ul>
@endforeach

The title and text property you see there, simply need to be the column names you have in your database. You should change those to match yours.

Using the example above, you should be able to style it how you want and place it in a form. Right now it will display the question within an h3 tag, then a unordered list with the answers below that.

Upvotes: 3

Related Questions