如何检查其他表上的值?

时间:2019-05-13 11:05:02

标签: php laravel

我有2个表,一个叫“考试”,另一个叫“考试结果”。在考试中,我输入passing_score,在考试结果中,保存考试答案。我正在尝试查找passing_score,如果合格分数更高,那么结果就是合格。

这是这段代码的全部功能

public function exam($course_id, Request $request)
        {
            $course = Course::where('id', $course_id)->firstOrFail();
            $answers = [];
            $exam_score = 0;
            foreach ($request->get('question') as $question_id => $answer_id) {
                $question = ExamQuestion::find($question_id);
                $correct_answer = ExamOption::where('exam_question_id', $question_id)
                    ->where('id', $answer_id)
                    ->where('is_correct', 1)->count() > 0;
                $answers[] = [

                    'exam_question_id' => $question_id,
                    'exam_option_id' => $answer_id,
                    'corect' => $correct_answer
                ];
                if ($correct_answer) {
                    $exam_score += $question->score;
                }
            }

            $exam_result = ExamResult::create([
              'exam_id' => $course->exam->id,
              'employee_id' => \Auth::id(),
              'result' => $exam_score,
            ]);
            $exam_result->answers()->createMany($answers);

            $exam_id = ExamResult::all();

            $final_results = Exam::where('id', $exam_id)->get(['passing_grade']);
            $val = $final_results->passing_grade;


            if($exam_result->result >= $val) {
              $exam_result->is_complete = 1;
              $exam_result->save();
}

            return redirect()->route('learn.show', [$course, $request])->with('message', 'Test score: ' . $exam_score);
        }

这是我尝试过的逻辑,我被困住了。

  $exam_id = ExamResult::all();
  $final_results = Exam::where('id', $exam_id)->get('passing_grade');
  $val = $final_results->passing_grade;

            if($exam_result->result >= $val) {
              $exam_result->is_complete = 1;
              $exam_result->save();
}

这是我遇到的错误

  

传递给Illuminate \ Database \ Grammar :: columnize()的参数1必须为   类型数组的给定字符串,在   /Applications/MAMP/htdocs/QuickHS/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php   在第137行

这是2个表的数据库结构 这是检查表 enter image description here

这是考试成绩表 enter image description here

2 个答案:

答案 0 :(得分:0)

这是更正。请尝试这个。

 $final_results = Exam::where('id', $exam_id)->first();
 $val = $final_results->passing_grade;

答案 1 :(得分:0)

我已经解决了,这是我的方法

     $final_results= Exam::first();
     $x = $final_results->passing_grade;

      if($exam_result->result >= $x) 
        {
               $exam_result->is_complete = 1;
                  $exam_result->save();
   }