代码之家  ›  专栏  ›  技术社区  ›  toto01

将用户的选择与数据库中的记录与Laravel进行比较

  •  0
  • toto01  · 技术社区  · 6 年前

    我有4个单选按钮,用户可以选择其中的一个选项,我想检查这个选项是否存在于表问题中(这个表包含问题的文本、选项和更正答案),如果存在,我想增加表测试中的字段点,并且控制器包含:

    $userChoise = $request->answer;
    $test = new Test;
    $questions = DB::table('question')->get();
    
        foreach ($questions as $question) {
           if ($question->correcte == $userChoise) {
    
                 $test->nbr_points = 10;
                 $test->save();
    
             }else{
    
                 $test->nbr_points = 0;
                 $test->save();
                }
            }
    

    问题 如果它仍在运行else处理,则不考虑条件中的内容

    1 回复  |  直到 6 年前
        1
  •  0
  •   MyLibary    6 年前

    $userChoise = $request->answer;
    $test = new Test;
    $correctAnswers = DB::table('question')->where('correcte', $userChoise)->count();
    
    $test->nbr_points = $correctAnswers * 10;
    $test->save();