检索与多个记录关联的多个记录laravel 4.2 [英] retrieving multiple records associated to multiple records laravel 4.2
问题描述
最近我问这个问题有一个答案,但不幸的是没有解决问题,之后我没有更多的答案,我真的需要解决这个问题我有。
recently i asked this question which got one answer but unfortunately didn't solve the problem, after that i got no more answers and i really need to fix this problem i have.
好的,所以我必须为我的学校做一个测验网站,用户应该可以在其中玩测验,这个页面需要显示测验名称,与测验相关的问题以及与问题相关的答案。
我可以显示测验名称没有问题,我可以显示问题,但由于某些原因,只显示与最终问题相关的答案。
Alright so i have to make a quiz website for my school in which a user should be able to play the quiz, this page need to show the quiz name, the questions associated to the quiz and the answers associated to the questions. I can show the quiz name no problem and i can show the questions aswell, but for some reason only the answers associated to the final question are shown.
这是我的代码:
public function playQuiz($id)
{
// get all the questions associated to the selected quiz
$questions = Question::where('quiz_id', $id)->get();
// get all the answers associated to the questions
foreach($questions as $question)
{
$answers = Answer::where('question_id', $question->id)->get();
}
$data = [
'quiz' => Quiz::find($id),
'questions' => $questions,
'answers' => $answers
];
return View::make("quizzes.playQuiz", $data);
}
$ id变量是我选择的测验的ID,所以我应该能够检索与此ID相关联的所有数据,以及与此ID相关联的数据相关联的所有数据。
The $id variable is the id of the quiz i selected so i should be able to retrieve all data associated to this id, and all data associated to the associated data of this id.
这是我的html(带刀片):
Here's my html (with blade):
<h3>{{ $quiz->name }}</h3>
@foreach($questions as $question)
<h4>{{ $question->question }}</h4>
@foreach($answers as $answer)
@if($answer->question_id == $question->id)
<p>{{ $answer->answer }}</p>
@endif
@endforeach
@endforeach
我知道问题在于我从数据库获取答案的方式,但我不知道如何解决这个问题。帮助非常感谢!感谢您阅读!
I know that the problem lies within the way i get my answers from the db but i don't know how to fix it. Help is much appreciated! Thanks for reading!
*编辑,
我的数据库方案如下:
我有
- 一个名为测验的表格,名称和描述。
- 一个名为问题的表,一个问题和一个外键quiz_id
- 一个名为answer的答案和一个外键question_id / li>
- a table called quizzes with an id, name and description.
- a table called questions with an id, question and a foreign key "quiz_id"
- a table called answers with an id, answer and a foreign key "question_id"
一个测验可以有多个问题,但问题只能有一个测验,
a问题可以有多个答案,但答案可以只有一个问题。
a quiz can have multiple questions but a question can only have one quiz, a question can have multiple answers but an answer can only have one question.
我希望这是足够的关于我的数据库的信息,谢谢帮助!
i hope that is enough information about my database, thanks for helping out!
推荐答案
你应该使用Eloquent的关系来解决这个问题。请参阅这里: http://laravel.com/docs/4.2/eloquent#relationships
You should use Eloquent's relationships to solve this problem. See more here: http://laravel.com/docs/4.2/eloquent#relationships
我现在看到的方式是你有三个模型,你正在使用:测验
,问题
和答案
- 对吗?
The way I see it currently is that you have three models that you're working with: Quiz
, Question
and Answer
- right?
从你的问题我收集以下内容:
From your question I gather the following:
- 一个
测验
将有很多问题
s -
答案
将属于一个问题
- A
Quiz
will have manyQuestion
s - An
Answer
will belong to oneQuestion
所以基于这些假设,我会把模型看成如此...
So based on these assumptions I'd flesh out the models as so...
注意:
- 我一直没有使用4.3,所以你可能需要修改一些代码,但是应该是可以的。
- 下面的模型假设你是用雄辩的期望使用外键,如果不是,你可以将它们定义为第二个关系方法的参数(hasMany,belongs sTo)
Quiz.php
<?php
class Quiz extends Eloquent {
protected $table = 'quiz'; // or whatever your table is
public function questions()
{
return $this->hasMany('Question'); // this should be the model name
}
}
Question.php
<?php
class Question extends Eloquent {
protected $table = 'question'; // or whatever your table is
public function quiz()
{
return $this->belongsTo('Quiz'); // defining the inverse of the relation
}
public function answers()
{
return $this->hasMany('Answer');
}
}
Answer.php
<?php
class Answer extends Eloquent {
protected $table = 'answer'; // or whatever your table is
public function question()
{
return $this->belongsTo('Question');
}
}
然后您的控制器变为很多清洁
Then your controller becomes a lot cleaner
控制器
public function playQuiz($id)
{
$quiz = Quiz::find($id);
return View::make('quizzes', compact('quiz'));
}
查看
<h3>{{ $quiz->name }}</h3>
@foreach($quiz->questions as $question)
<h4>{{ $question->question }}</h4>
@foreach($question->answers as $answer)
<p>{{ $answer->answer }}</p>
@endforeach
@endforeach
请让我知道如果您在执行上述任何问题上都遇到困难,我会尽力帮助您。首先,关系可能有点棘手,但是一旦你得到你的头,你永远不会回头。
Please let me know if you have any trouble implementing the above and I'll do my best to help out. Relationships can be a bit tricky at first but once you get your head round them you'll never look back.
这篇关于检索与多个记录关联的多个记录laravel 4.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!