How to get specific class data using Laravel Rloquent?

What I want :

  • To get a grade of what the user answered in the database, if one exists, and get an estimate of this answer.

What I have :

  • I have tables 3 , short_answer , sa_sa_answer , short_answer_answer .
  • Now the short_answer table has a question, and each question has many answers located in the short_answer_answer table, and the class is also included there, the question can have 1 or more answers.

What is my code:

controller

 foreach($sa_question_id as $key => $value){ $sa = ShortAnswer::with('answers')->find($sa_question_id[$key]); $possible_answers = []; foreach($sa->answers as $possible_answer){ $possible_answers[] .= strtolower($possible_answer->answer); } if(in_array(strtolower($sa_answer[$key]), $possible_answers)){ $grade = ShortAnswerAnswer::where('answer', $sa_answer[$key])->get(); echo "plus +1. Grade is: " . $grade->grade . "<br>"; } } 

The problem is this:

I just get the answer where the answer is equal to the user's response. But what if I have TWO strong> the same answer and a different grade and obviously a different question. He may choose the wrong one.

Note: I am using Laravel5.1

Update: Table Structure

short_answer
- name
- question

sa_sa_answer
- short_answer_id
- short_answer_answer_id

short_answer_answer
- answer
- grade

Refresh

I already solved this problem, but no one received the generosity, but if you could answer this question , I can give you the generosity plus 2 , and I really need additional help. This is also related to this issue. Now the bounty will disappear after 3 days.

+8
php eloquent laravel-5
source share
2 answers

We can solve this problem using one request:

 $questions = ShortAnswer::with('answers') ->whereIn('id', $sa_question_id) ->whereHas('answers', function ($query) use ($sa_answer) { $placeholders = join(",", array_pad([], count($sa_answer), "?")); $query->whereRaw("LOWER(answer) IN $placeholders", $sa_answer); }) ->get(); foreach ($questions as $question) { foreach ($question->answers as $answer) { echo 'plus +1. Grade is: ' + $answer->grade}."; } } 

This optimizes the loop, so we only need to query the database once for all questions and answers. It also solves the problem in the question because the request maintains a relationship between the question and the answer, so we will not inadvertently choose the wrong answers to the questions.

+2
source share

This part of your code:

 $grade = ShortAnswerAnswer::where('answer', $sa_answer[$key])->get(); 

You get all answers from ShortAnswerAnswer that are equal to $sa_answer[$key] , there is no condition to indicate if there is an answer that you received from the corresponding question that you expected.

So, to solve your problem, you must also point out this question. How:

 $grade = ShortAnswerAnswer::where(['answer' => $sa_answer[$key], 'question_id' => $key])->get(); 

P / S: This part:

 foreach($sa_question_id as $key => $value){ $sa = ShortAnswer::with('answers')->find($sa_question_id[$key]); ... } 

Then $sa_question_id[$key] exactly $value .

+1
source share

All Articles