form->fill(); } protected function getFormSchema(): array { return [ Forms\Components\Select::make('teacherSubjectId') ->label('Teacher Subject') ->options( TeacherSubject::with(['teacher', 'subject', 'class'])->get()->mapWithKeys(function ($item) { return [ $item->id => "{$item->teacher->name} - {$item->subject->name} - {$item->class->class_name}" ]; })->toArray() ) ->searchable() ->reactive() ->afterStateUpdated(fn ($state) => $this->loadStudents()), ]; } public function loadStudents(): void { if (!$this->teacherSubjectId) { $this->students = []; return; } $teacherSubject = TeacherSubject::find($this->teacherSubjectId); if (!$teacherSubject) { $this->students = []; Notification::make() ->title('Not Found') ->body('Selected teacher subject not found.') ->danger() ->send(); return; } $this->students = Student::where('class_id', $teacherSubject->class_id) ->get() ->map(function ($student) { $existingAssessment = Assessment::where('student_id', $student->id) ->where('teacher_subject_id', $this->teacherSubjectId) ->first(); return [ 'id' => $student->id, 'name' => $student->full_name, 'nis' => $student->nis, 'score' => $existingAssessment ? $existingAssessment->score : null, ]; })->toArray(); } public function submit(): void { if (!$this->teacherSubjectId || empty($this->students)) { Notification::make() ->title('Error') ->body('Please select a teacher subject and enter student scores.') ->danger() ->send(); return; } foreach ($this->students as $student) { if (!isset($student['score'])) continue; Assessment::updateOrCreate( [ 'student_id' => $student['id'], 'teacher_subject_id' => $this->teacherSubjectId, ], [ 'score' => $student['score'], ] ); } Notification::make() ->title('Success') ->body('Assessments saved successfully.') ->success() ->send(); } protected function getHeaderActions(): array { return [ Actions\Action::make('back') ->label('Back to Assessments') ->url(static::getResource()::getUrl('index')), ]; } }