如何使用Laravel Eloquent获取特定的成绩数据? [英] How to get a specific grade data using Laravel Eloquent?

查看:79
本文介绍了如何使用Laravel Eloquent获取特定的成绩数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的:

  1. 要获取用户在数据库中给出的答案的grade(如果存在)并获得答案的等级.
  1. To get the grade of what the user answered in the database if exists and get the grade of that answer.

我有什么:

  1. 我有3个表,short_answersa_sa_answershort_answer_answer.
  2. 现在,short_answer表中有问题,并且每个问题在short_answer_answer表中都有很多答案,并且其中还包括分数,该问题可以有 1个或更多答案.
  1. I have 3 tables, short_answer, sa_sa_answer, short_answer_answer.
  2. Now, short_answer table has question, and each question has many answer located in the short_answer_answer table and the grade is also included there, the question can have 1 or more answer.

我有什么代码:

控制器

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

问题是:

我只是得到答案等于用户答案的​​答案.但是,如果我有两个相同的答案,不同的grade和明显不同的问题,该怎么办?它可以选择错误的一个.

Im just getting the answer where the answer is equal to the user's answer. But what if I have TWO same answer and different grade and obviously different question. It can select the wrong one.

注意: 我正在使用Laravel5.1

更新:表结构

short_answer
-name
-question

short_answer
- name
- question

sa_sa_answer
-short_answer_id
-short_answer_answer_id

sa_sa_answer
- short_answer_id
- short_answer_answer_id

short_answer_answer
-answer
-grade

short_answer_answer
- answer
- grade

Update

我已经解决了这个问题,但是没有人得到赏金,但是如果您可以回答

I've already solved this issue, however no one got the bounty, but If you could answer this question, I can give you the bounty plus 2 check mark and up vote, I just really need more help with this. It is also connected with this question. The bounty will be gone in 3 days from now.

推荐答案

我们可以使用一个查询来解决此问题:

We can solve this problem using a single query:

$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 out the loop so we only need to query the database once for all the questions and answers. It also solves the problem in the question because the query maintains the relationships between question and answer, so we won't inadvertently select the wrong answers for the questions.

这篇关于如何使用Laravel Eloquent获取特定的成绩数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆