duanchuang1935 2015-05-29 12:49
浏览 45

从db获取与先前所选行关联的多行。 laravel 4.2 php

so here is my problem,

for a school project i have to make a quiz website in which a normal user should be able to play a quiz, so i have to show all the questions associated to the quiz and all the answers associated to the question, but when i try to do so it only shows the answers that are associated to the final question. here is my code:

    // select the clicked quiz.
    $quiz = Quiz::find($id);

    // get all the question and answer records from db that are associated to the selected quiz.
    $questions = DB::table('questions')->where('quiz_id', $id)->get();

    foreach($questions as $question)
    {
        $answers = DB::table('answers')->where('question_id', $question->id)->get();
    }

    return View::make("quizzes.makeQuiz", [
        "quiz" => Quiz::find($id)
    ])->with('quiz', $quiz)->with('questions', $questions)->with('answers', $answers);

and here is the html (using blade):

    <h3>{{ $quiz->name }}</h3><br/>

    @foreach($questions as $question)

        <h4>{{ $question->question }}</h4>

        @foreach($answers as $answer)


                <p>{{ $answer->answer }}</p>



        @endforeach            

    @endforeach

when i try to do this without a foreach i get an error that says 'Trying to get property of non-object'. i know why i get that error because obviously $questions isn't an object.

help is very much appreciated! thanks for reading!

edit*

alright so i have now changed my code to look like this:

    $questions = Question::where('quiz_id', $id)->get();

    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.makeQuiz", $data);

the html is basically the same, it returns the quiz and all the questions associated. but unfortunately it only returns the answers associated to the final questions while each questions has 3 answers. i think the problem lies in $answers but i don't know exactly what i am doing wrong here :( please help! thanks!

*edit

I asked this question a second time and it solved my problem, here is the link: retrieving multiple records associated to multiple records laravel 4.2

  • 写回答

1条回答 默认 最新

  • dtz46697 2015-05-29 13:45
    关注

    Rewrite your return statement as such:

    $data = [
        'quiz'      => Quiz::find($id),
        'questions' => $questions,
        'answers'   => $answers
    ];
    
    return View::make("quizzes.makeQuiz", $data);
    

    You are currently returning an array AND trying to return chained variables. Most likely your $questions variable never reaches the view because you are passing an array first.

    Plus the fact you were trying to collect the Quiz model twice for some reason is just wrong.

    Also - why are you mixing Eloquent and Fluent statements to get your models? Stick to one or the other unless the situation warrants it.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题