检索与多个记录关联的多个记录 laravel 4.2
retrieving multiple records associated to multiple records laravel 4.2
最近我问了这个问题,得到了一个答案,但不幸的是没有解决问题,之后我没有得到更多的答案,我真的需要解决这个问题。
好的,所以我必须为我的学校制作一个测验网站,用户应该可以在其中玩测验,这个页面需要显示测验名称、与测验相关的问题以及与问题。
我可以毫无问题地显示测验名称,也可以显示问题,但出于某种原因,只显示与最终问题相关的答案。
这是我的代码:
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 的关联数据关联的所有数据。
这是我的 html(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
我知道问题在于我从数据库中获取答案的方式,但我不知道如何解决。非常感谢您的帮助!感谢阅读!
*编辑,
我的数据库方案如下:
我有
- a table 调用了带有 ID、名称和描述的测验。
- a table 用 id、question 和外键调用了 questions "quiz_id"
- a table 使用 ID、答案和外键调用答案 "question_id"
一个测验可以有多个问题,但一个问题只能有一个测验,
一个问题可以有多个答案,但一个答案只能有一个问题。
我希望这是关于我的数据库的足够信息,感谢您的帮助!
你应该使用Eloquent的关系来解决这个问题。在此处查看更多信息:http://laravel.com/docs/4.2/eloquent#relationships
我目前的看法是您正在使用三个模型:Quiz
、Question
和 Answer
- 对吗?
根据你的问题,我收集到以下信息:
- 一个
Quiz
会有很多Question
个
- 一个
Answer
将属于一个Question
因此,基于这些假设,我会充实模型……[=23=]
注:
- 我有一段时间没有使用 4.3,所以您可能需要更改一些代码,但应该没问题
- 下面的模型假定您正在以 eloquent 期望的方式使用外键,如果不是,您可以将它们定义为关系方法的第二个参数 (hasMany, belongsTo)
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');
}
}
然后你的控制器变得很多更清洁
控制器
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
如果您在执行上述内容时遇到任何问题,请告诉我,我会尽力提供帮助。人际关系一开始可能有点棘手,但一旦你了解它们,你就再也不会回头了。
最近我问了这个问题,得到了一个答案,但不幸的是没有解决问题,之后我没有得到更多的答案,我真的需要解决这个问题。
好的,所以我必须为我的学校制作一个测验网站,用户应该可以在其中玩测验,这个页面需要显示测验名称、与测验相关的问题以及与问题。 我可以毫无问题地显示测验名称,也可以显示问题,但出于某种原因,只显示与最终问题相关的答案。
这是我的代码:
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 的关联数据关联的所有数据。
这是我的 html(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
我知道问题在于我从数据库中获取答案的方式,但我不知道如何解决。非常感谢您的帮助!感谢阅读!
*编辑,
我的数据库方案如下:
我有
- a table 调用了带有 ID、名称和描述的测验。
- a table 用 id、question 和外键调用了 questions "quiz_id"
- a table 使用 ID、答案和外键调用答案 "question_id"
一个测验可以有多个问题,但一个问题只能有一个测验, 一个问题可以有多个答案,但一个答案只能有一个问题。
我希望这是关于我的数据库的足够信息,感谢您的帮助!
你应该使用Eloquent的关系来解决这个问题。在此处查看更多信息:http://laravel.com/docs/4.2/eloquent#relationships
我目前的看法是您正在使用三个模型:Quiz
、Question
和 Answer
- 对吗?
根据你的问题,我收集到以下信息:
- 一个
Quiz
会有很多Question
个 - 一个
Answer
将属于一个Question
因此,基于这些假设,我会充实模型……[=23=]
注:
- 我有一段时间没有使用 4.3,所以您可能需要更改一些代码,但应该没问题
- 下面的模型假定您正在以 eloquent 期望的方式使用外键,如果不是,您可以将它们定义为关系方法的第二个参数 (hasMany, belongsTo)
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');
}
}
然后你的控制器变得很多更清洁
控制器
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
如果您在执行上述内容时遇到任何问题,请告诉我,我会尽力提供帮助。人际关系一开始可能有点棘手,但一旦你了解它们,你就再也不会回头了。