php - retrieving multiple records associated to multiple records laravel 4.2 -
recently asked question got 1 answer unfortunately didn't solve problem, after got no more answers , need fix problem have.
alright have make quiz website school in user should able play quiz, page need show quiz name, questions associated quiz , answers associated questions. can show quiz name no problem , can show questions aswell, reason answers associated final question shown.
here's code:
public function playquiz($id) { // questions associated selected quiz $questions = question::where('quiz_id', $id)->get(); // answers associated questions foreach($questions $question) { $answers = answer::where('question_id', $question->id)->get(); } $data = [ 'quiz' => quiz::find($id), 'questions' => $questions, 'answers' => $answers ]; return view::make("quizzes.playquiz", $data); }
the $id variable id of quiz selected should able retrieve data associated id, , data associated associated data of id.
here's html (with blade):
<h3>{{ $quiz->name }}</h3> @foreach($questions $question) <h4>{{ $question->question }}</h4> @foreach($answers $answer) @if($answer->question_id == $question->id) <p>{{ $answer->answer }}</p> @endif @endforeach @endforeach
i know problem lies within way answers db don't know how fix it. appreciated! reading!
*edit,
my db scheme goes follows:
i have
- a table called quizzes id, name , description.
- a table called questions id, question , foreign key "quiz_id"
- a table called answers id, answer , foreign key "question_id"
a quiz can have multiple questions question can have 1 quiz, question can have multiple answers answer can have 1 question.
i hope enough information database, helping out!
you should use eloquent's relationships solve problem. see more here: http://laravel.com/docs/4.2/eloquent#relationships
the way see have 3 models you're working with: quiz
, question
, answer
- right?
from question gather following:
- a
quiz
have manyquestion
s - an
answer
belong 1question
so based on these assumptions i'd flesh out models so...
note:
- i haven't used 4.3 while may need alter of code, should okay
- models below assume using foreign keys in manner eloquent expects too, if not can define them second argument on relationship method (hasmany, belongsto)
quiz.php
<?php class quiz extends eloquent { protected $table = 'quiz'; // or whatever table public function questions() { return $this->hasmany('question'); // should model name } }
question.php
<?php class question extends eloquent { protected $table = 'question'; // or whatever table public function quiz() { return $this->belongsto('quiz'); // defining inverse of relation } public function answers() { return $this->hasmany('answer'); } }
answer.php
<?php class answer extends eloquent { protected $table = 'answer'; // or whatever table public function question() { return $this->belongsto('question'); } }
then controller becomes a lot cleaner
controller
public function playquiz($id) { $quiz = quiz::find($id); return view::make('quizzes', compact('quiz')); }
view
<h3>{{ $quiz->name }}</h3> @foreach($quiz->questions $question) <h4>{{ $question->question }}</h4> @foreach($question->answers $answer) <p>{{ $answer->answer }}</p> @endforeach @endforeach
please let me know if have trouble implementing above , i'll best out. relationships can bit tricky @ first once head round them you'll never back.
Comments
Post a Comment