2015-05-31 102 views
0

最近我问了这个问题,得到了一个答案,但不幸的是没有解决问题,之后,我没有更多的答案,我真的需要解决这个问题,我有。检索多个记录相关的多个记录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(带刀片):

<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 

我知道问题出在我从DB我的答案的方式中,但我不知道如何解决它。非常感谢帮助!谢谢阅读!

*编辑,

我的DB计划去如下:

我有

  • 表调用的ID,名称和说明测验。
  • 一个ID,问题和外键的表称为问题“quiz_id”
  • 表调用一个id,答案和外键的回答“question_id”

测验可以有多个问题但一个问题只能有一个测验, 一个问题可以有多个答案,但答案只能有一个问题。

我希望这是关于我的数据库的足够信息,感谢您的帮助!

+1

问题在于你正在写'$ answer'变量而不是添加到它的foreach循环,这就是为什么你只能看到最后一个。无论如何,我认为这是错误的方法,你应该利用Eloquents关系功能。我现在尝试抛出一个答案,但是你可能需要通过编辑和更新你的问题来解释你的数据库模式。 – haakym

+0

感谢您对数据库架构的更新。我相信我不应该更新我的答案,因为我所做的假设与您描述数据库的方式非常相似。我在4.3版本中试图做到这一点,但是如果你看到某些看起来不正确的东西,比如你在模型中扩展了什么类,那么就忽略它。希望你能使它工作,如果你需要帮助,请留下评论。谢谢。 – haakym

回答

0

你应该使用Eloquent的关系来解决这个问题。在这里看到更多:http://laravel.com/docs/4.2/eloquent#relationships

我目前看到的方式是,您有三种型号,您正在使用:Quiz,QuestionAnswer - 对不对?

从你的问题我备齐下列各项:

  • 一个Quiz将有许多Question小号
  • Answer将属于一个Question

因此,基于这些假设我会割肉这样的模型...

注:

  • 我没有用4.3一会儿,所以你可能需要修改一些代码,但它应该没关系下面
  • 模型假设您正在使用外键以这样的方式口才期望你太,如果没有可以将它们定义为在关系方法的第二个参数(的hasMany,属于关联)

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 

请让我知道你是否有实施上述任何问题,我会尽我所能来助阵。关系一开始可能有点棘手,但一旦你把头转过来,你就永远不会回头。