dsvyc66464 2019-05-25 11:45
浏览 37
已采纳

如何显示多个foreach?

I have changed the model Exams to save ExamQuestions from hasMany to belongsTo and here is how I've modified my controller. I'm getting a Invalid argument supplied for foreach() in the View.

Here is the Exam model that I've changed

     public function questions()
  {
      return $this->belongsTo(ExamQuestion::class, 'exam_questions');
  }

Here is my controller

public function exam($course_id, Request $request)
        {
            $course = Course::where('id', $course_id)->firstOrFail();
            $answers = [];
            $exam_score = 0;
            foreach ($request->get('questions') 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);

            $get_reslts_score= Exam::with('exam_results')->first();
            $x = $get_reslts_score->passing_grade;

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

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

Here is my view

  <h3>@if ($courses->exam)</h3>
  <hr/>
    <div class="row">
      <div class="col-xs-12 form-group">

      <form action="{{ route('exam.save', [$courses->id]) }}" method="post">
      {{ csrf_field() }}


        @foreach($courses->exam->questions as $question)

        <br>{{$loop->iteration}} . {{$question->question}}</b>
        </br>

        @foreach($questions->exam_options as $option)
        &nbsp;<input type="radio" name="question[{{ $question->id }}]" value="{{ $option->id }}"/>&nbsp;&nbsp;{{ $option->text }}</br>
        @endforeach


        <br>
        @endforeach
  • 写回答

1条回答 默认 最新

  • dsgm5631 2019-05-25 17:49
    关注
      <h3>@if ($courses->exam)</h3>
      <hr/>
      <div class="row">
          <div class="col-xs-12 form-group">
             <form action="{{ route('exam.save', [$courses->id]) }}" method="post">
                  {{ csrf_field() }}
               @foreach($courses->exam->questions as $questions)
                   <br>{{$loop->iteration}} . {{$questions->question}}</b>
                   </br>
    
                @foreach($questions->exam_options as $option)
                   &nbsp;<input type="radio" name="question[{{ $questions->id }}]" value="{{ $option->id }}"/>&nbsp;&nbsp;{{ $option->text }}</br>
                @endforeach
                <br>
              @endforeach
    

    If it did not work please check your model relationships returning value or not. Using parent to recursively go through the full array you can go up to any level of depth here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ue5运行的通道视频都会有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数