database - getting multiple rows from db that are associated to previous selected rows. laravel 4.2 php -
so here problem,
for school project have make quiz website in normal user should able play quiz, have show questions associated quiz , answers associated question, when try shows answers associated final question. here code:
// select clicked quiz. $quiz = quiz::find($id); // question , answer records db associated selected quiz. $questions = db::table('questions')->where('quiz_id', $id)->get(); foreach($questions $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 html (using blade):
<h3>{{ $quiz->name }}</h3><br/> @foreach($questions $question) <h4>{{ $question->question }}</h4> @foreach($answers $answer) <p>{{ $answer->answer }}</p> @endforeach @endforeach
when try without foreach error says 'trying property of non-object'. know why error because $questions isn't object.
help appreciated! reading!
edit*
alright have changed code this:
$questions = question::where('quiz_id', $id)->get(); foreach($questions $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 same, returns quiz , questions associated. unfortunately returns answers associated final questions while each questions has 3 answers. think problem lies in $answers don't know doing wrong here :( please help! thanks!
*edit
i asked question second time , solved problem, here link: retrieving multiple records associated multiple records laravel 4.2
rewrite return statement such:
$data = [ 'quiz' => quiz::find($id), 'questions' => $questions, 'answers' => $answers ]; return view::make("quizzes.makequiz", $data);
you returning array , trying return chained variables. $questions
variable never reaches view because passing array first.
plus fact trying collect quiz
model twice reason wrong.
also - why mixing eloquent
, fluent
statements models? stick 1 or other unless situation warrants it.
Comments
Post a Comment