I'm trying to show a list of question and his respective answers. I store all on 3 different database tables, questions>type of answer>list of possible answers.
This is what I've done on the controller:
public function show()
{
$questions = DB::table('questions')
->join('typeAnswers', 'typeAnswers.id', '=', 'questions.answer_id')
->join('answers', 'answers.typeAnswers_id', '=', 'typeAnswers.id')
->get();
return view('questions.showquestions', ['questions' => $questions]);
}
On the view I use a foreach iteration to show all the questions:
@extends('welcome')
@section('content')
@foreach($questions as $question)
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{$question->statement}}</h3>
</div>
<div class="panel-body">
{{$question->value}}
</div>
</div>
@endforeach
@endsection
On the database for testing I just have one question, and the possible answers are Yes or No. But instead of see one question and two possible answers, I get the same question 2 times and each time a one different answer.
Check this query:
$questions = DB::table('questions')
->join('typeAnswers', 'typeAnswers.id', '=', 'questions.answer_id')
->join('answers', 'answers.typeAnswers_id', '=', 'typeAnswers.id')
->get();
You are joining questions
table with typeAnswers
so the result here is some thing like:
Q1 A1
Q1 A2
And on view the required result is like:
Q1 [A1, A2]
So that you can show answers under one question. Modify your query accordingly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.