如何在Laravel中为学生分配分数?

9rygscc1  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(111)

我在Laravel中创建了这个学生管理系统,我完成了学生管理功能,教师可以对学生执行CRUD,与科目相同。然而,我需要创建一个评估功能,允许教师在每次课堂活动后为学生分配分数。我已经能够创建一些东西,但它根本不起作用,我尝试了所有形式的调试,但不知道为什么它不工作。我需要帮助。这是我到目前为止。
当前,当我尝试创建新评估时,收到此错误。SQLSTATE[HY000]: General error: 1364 Field 'student_id' doesn't have a default value (Connection: mysql, SQL: insert into评估( assessment_name , score , score_over , user_id , updated_at , created_at ) values (Homework11, 30,24, 40, 1, 2023-04-07 18:02:42, 2023-04-07 18:02:42))
// AssessmentController store() method

public function store(StoreAssessmentRequest $request)
{
    $validated = $request->validated();

    // Create the assessment instance
    $assessment = new Assessment();
    $assessment->assessment_name = $validated['assessment_name'];
    $assessment->score = implode(',', $validated['score']);
    $assessment->score_over = $validated['score_over'];
    $assessment->user_id = auth()->user()->id;

    // Save the assessment instance to the database
    if (!$assessment->save()) {
        // handle error, for example:
        return back()->withInput()->withErrors(['error' => 'Failed to save assessment']);
    }

    $assessment_id = $assessment->id;

    // Save the subject for the assessment
    $subject = Subject::where('name', $validated['subject'])->firstOrFail();
    $assessment->subjects()->syncWithoutDetaching($subject->id);

    // Save the scores for each student
    foreach ($validated['score'] as $student_id => $score) {
        $student = Student::findOrFail($student_id);
        $assessment->students()->syncWithoutDetaching([
            $student->id => [
                'score' => $score,
                'subject_id' => $subject->id,
                'assessment_id' => $assessment_id,
                'student_id' => $student_id,
            ]
        ]);
    }

    return redirect()->route('assessments.index')->with('success', 'Assessment saved successfully.');
}

// create.blade.php在输入学生分数部分,我从student表中获取所有可用的学生,并为他们分配不同的分数。

<div class="form-group">
    <h3>Enter Scores of students</h3>
     @foreach($students as $index => $student)
       <label for="score-{{ $student->id }}">{{ $student->firstname }} {{ $student->lastname }}</label>
       <input type="number" class="form-control" id="score-{{ $student->id }}" name="score[{{ $student->id }}]" max="100" required>
      @endforeach
 </div>

// Validation in StoreAssessmentRequest

public function rules(): array
    {
        return [
            'assessment_name' => 'required',
            'score' => 'required|array',
            'score_over' => 'required|integer|min:0|max:100',
            'subject' => 'required|exists:subjects,name',
            'score.*' => 'required|integer|min:0|max:100',
        ];
    }
ffx8fchx

ffx8fchx1#

然后,验证器中的请求输入名称是scores,但您在foreach循环中使用scores

foreach ($validated['score'] /* Change here to $validated['scores'] */ as $student_id => $score) {
            if($student = Student::findOrFail($student_id)){
            $assessment->students()->syncWithoutDetaching($student->id, ['score' => $score, 'subject_id' => $subject->id]);
            }
        }

相关问题