douyun1852 2015-05-31 09:45
浏览 36
已采纳

检索与多个记录相关的多个记录

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.

Here's my code:

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);
}

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.

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!

*edit,

my db scheme goes as follows:

i have

  • 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 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!

  • 写回答

1条回答 默认 最新

  • duanlieshuang5330 2015-05-31 10:32
    关注

    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:

    • A Quiz will have many Questions
    • An Answer will belong to one Question

    So based on these assumptions I'd flesh out the models as so...

    Note:

    • I haven't used 4.3 for a while so you may need to alter some of the code, but it should be okay
    • Models below assume you are using foreign keys in the manner eloquent expects you too, if not you can define them as the second argument on the relationship method (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');
        }
    
    }
    

    Then your 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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重