【问题标题】:retrieving multiple records associated to multiple records laravel 4.2检索与多条记录关联的多条记录laravel 4.2
【发布时间】:2015-08-13 21:08:20
【问题描述】:

最近我问了这个问题,得到了一个答案,但不幸的是没有解决问题,之后我没有得到更多答案,我真的需要解决这个问题。

好的,所以我必须为我的学校制作一个测验网站,用户应该能够在其中玩测验,此页面需要显示测验名称、与测验相关的问题以及与问题相关的答案。 我可以显示测验名称没有问题,我也可以显示问题,但由于某种原因,只显示与最终问题相关的答案。

这是我的代码:

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

我知道问题在于我从数据库获得答案的方式,但我不知道如何解决它。非常感谢您的帮助!感谢阅读!

*编辑,

我的数据库方案如下:

我有

  • 一个名为 quizzes 的表格,带有 ID、名称和描述。
  • 一个名为 questions 的表,带有一个 id、question 和一个外键“quiz_id”
  • 一个名为 answers 的表,带有一个 id、answer 和一个外键“question_id”

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

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

【问题讨论】:

  • 问题出在你的 foreach 循环中,你正在写 $answer 变量而不是添加到它,这就是为什么你只看到最后一个。无论如何,我认为这是错误的方法,您应该利用 Eloquents 的关系功能。我现在会尝试给出一个答案,但您可能需要通过编辑和更新您的问题来稍微解释一下您的数据库架构。
  • 感谢数据库架构的更新。我相信我不需要更新我的答案,因为我所做的假设几乎就是您描述数据库的方式。我在处理 4.3 时尝试这样做,但是如果您看到一些看起来不正确的东西,例如您在模型中扩展的类,那么请忽略它。希望你能得到它的工作,如果你需要帮助,请发表评论。谢谢。

标签: php laravel laravel-4 eloquent blade


【解决方案1】:

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

我目前的看法是,您正在使用三个模型:QuizQuestionAnswer - 对吗?

根据您的问题,我收集了以下信息:

  • 一个Quiz 会有很多Questions
  • Answer 将属于一个Question

因此,基于这些假设,我会像这样充实模型......

注意:

  • 我有一段时间没有使用 4.3,所以您可能需要更改一些代码,但应该没问题
  • 下面的模型假设您以 eloquent 期望的方式使用外键,否则您可以将它们定义为关系方法的第二个参数(hasMany、belongsTo)

测验.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

如果您在执行上述操作时遇到任何问题,请告诉我,我会尽力提供帮助。关系一开始可能有点棘手,但一旦你了解它们,你就永远不会回头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 2021-02-17
    • 2010-09-23
    相关资源
    最近更新 更多