$student, 'relations' => $this->relationOptions(), 'educationOptions' => $this->educationOptions(), ]); } public function store(Request $request, Student $student): RedirectResponse { $data = $this->validateParent($request, $student); $data['student_id'] = $student->id; StudentParent::create($data); Alert::success('Berhasil', 'Data orang tua/wali berhasil ditambahkan.'); return redirect()->route('students.show', $student)->with('success', 'Data orang tua/wali berhasil ditambahkan.'); } public function edit(Student $student, StudentParent $parent): View { abort_unless($parent->student_id === $student->id, 404); return view('student-parents.edit', [ 'student' => $student, 'parent' => $parent, 'relations' => $this->relationOptions(), 'educationOptions' => $this->educationOptions(), ]); } public function update(Request $request, Student $student, StudentParent $parent): RedirectResponse { abort_unless($parent->student_id === $student->id, 404); $data = $this->validateParent($request, $student, $parent); $parent->update($data); Alert::success('Berhasil', 'Data orang tua/wali berhasil diperbarui.'); return redirect()->route('students.show', $student)->with('success', 'Data orang tua/wali berhasil diperbarui.'); } public function destroy(Student $student, StudentParent $parent): RedirectResponse { $this->ensureAdmin(); abort_unless($parent->student_id === $student->id, 404); $parent->delete(); Alert::success('Berhasil', 'Data orang tua/wali berhasil dihapus.'); return redirect()->route('students.show', $student)->with('success', 'Data orang tua/wali berhasil dihapus.'); } protected function validateParent(Request $request, Student $student, ?StudentParent $parent = null): array { return $request->validate([ 'relation_type' => ['required', Rule::in(array_keys($this->relationOptions())), Rule::unique('student_parents', 'relation_type')->ignore($parent?->id)->where(fn ($query) => $query->where('student_id', $student->id))], 'name' => ['required', 'string', 'max:150'], 'nik' => ['nullable', 'string', 'max:30'], 'birth_year' => ['nullable', 'integer', 'digits:4'], 'education' => ['nullable', Rule::in(array_keys($this->educationOptions()))], 'occupation' => ['nullable', 'string', 'max:100'], 'phone_number' => ['nullable', 'string', 'max:30'], 'address' => ['nullable', 'string'], 'is_alive' => ['nullable', 'boolean'], ]); } protected function relationOptions(): array { return [ 'father' => 'Ayah', 'mother' => 'Ibu', 'guardian' => 'Wali', ]; } protected function educationOptions(): array { return [ 'Tidak Sekolah' => 'Tidak Sekolah', 'Tidak Tamat SD' => 'Tidak Tamat SD', 'SD' => 'SD', 'SMP' => 'SMP', 'SLTA' => 'SLTA', 'D1' => 'D1', 'D2' => 'D2', 'D3' => 'D3', 'D4' => 'D4', 'S1' => 'S1', 'S2' => 'S2', 'S3' => 'S3', 'Lainnya' => 'Lainnya', ]; } protected function ensureAdmin(): void { abort_unless(auth()->user()?->isAdmin(), 403, 'Aksi ini hanya boleh dilakukan administrator.'); } }