Как сохранить результаты на нескольких таблицах? - PullRequest
0 голосов
/ 07 мая 2019

Я пытаюсь сохранить экзамен для каждого курса, и вот что я попробовал. Я получаю сообщение об ошибке: страница 404 ошибки не найдена, а также данные не сохраняются в БД.

Вот мой контроллер

public function exam($course_id, Request $id)
        {
            $course = Course::where('id', $id)->firstOrFail();
            $answers = [];
            $exam_result = 0;
            foreach ($request->get('question') as $question_id => $answer_id) {
                $question = ExamQuestion::find($question_id);
                $correct_answer = ExamOption::where('exam_questions_id', $question_id)
                    ->where('id', $answer_id)
                    ->where('is_correct', 1)->count() > 0;
                $answers[] = [
                  //this comes from the exam_results_answers
                    'exam_question_id   ' => $question_id,
                    'exam_option_id' => $answer_id,
                    'corect' => $correct_answer
                ];
                if ($correct_answer) {
                    $exam_result += $question->score;
                }
            }
            //this comes from exam result and is correct
            $exam_result = ExamResult::create([
                'exam_id' => $course->exam->id,
                'employee_id' => \Auth::id(),
                'result' => $exam_result
            ]);
            $exam_result->answers()->createMany($answers);
            return redirect()->route('learn.show', [$course->curriculum_id, $id])->with('message', 'Test score: ' . $exam_resut);
        }

Вот клинок

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


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

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

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


                <br>
                @endforeach
              </br>
                 <input type="submit" value=" Submit results " />
               </form>

Вот маршруты

  Route::get('/exam/{id}', 'EmployeeCoursesController@view_exam')->name('exam.show');
Route::post('/exam/{id}', 'EmployeeCoursesController@exam')->name('exam.save');

1 Ответ

2 голосов
/ 07 мая 2019

Глядя на параметры в вашей exam() функции, я считаю, что эта строка:

$course = Course::where('id', $id)->firstOrFail();

На самом деле должно быть:

$course = Course::where('id', $course_id)->firstOrFail();

firstOrFail() выдаст 404, если не решит модель из базы данных.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...