feat: snapshot class_name di setiap presensi & pelanggaran
Migration: tambah kolom: - attendances.class_name (varchar 100) - violations.student_class (varchar 100) - Backfill data existing dari students.class_name Controller manual: simpan class_name dari form Sync E-Jurnal: simpan class_name dari lookup student ViolationService: ambil class_name dari student saat buat Model: tambah ke fillable Ini menjamin data historis tetap akurat meskipun siswa naik kelas (X→XI→XII) di tahun ajaran baru.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
// Add class_name to attendances
|
||||
Schema::table('attendances', function (Blueprint $table) {
|
||||
$table->string('class_name', 100)->nullable()->after('student_id');
|
||||
});
|
||||
|
||||
// Add student_class to violations (can't use 'class' — reserved word)
|
||||
Schema::table('violations', function (Blueprint $table) {
|
||||
$table->string('student_class', 100)->nullable()->after('student_id');
|
||||
});
|
||||
|
||||
// Backfill existing records with current student class_name
|
||||
DB::statement("UPDATE attendances a
|
||||
JOIN students s ON s.id = a.student_id
|
||||
SET a.class_name = s.class_name
|
||||
WHERE a.class_name IS NULL");
|
||||
|
||||
DB::statement("UPDATE violations v
|
||||
JOIN students s ON s.id = v.student_id
|
||||
SET v.student_class = s.class_name
|
||||
WHERE v.student_class IS NULL");
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('attendances', function (Blueprint $table) {
|
||||
$table->dropColumn('class_name');
|
||||
});
|
||||
|
||||
Schema::table('violations', function (Blueprint $table) {
|
||||
$table->dropColumn('student_class');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user