From 3910252c0e6561fba818eed3b390ddb68c7a3df6 Mon Sep 17 00:00:00 2001 From: "M. Iqbal Effendi" Date: Tue, 4 Jun 2024 13:58:52 +0800 Subject: [PATCH] generate student id number when it is empty --- app/Models/Student.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/Models/Student.php b/app/Models/Student.php index f6ad4de..8aeb12b 100644 --- a/app/Models/Student.php +++ b/app/Models/Student.php @@ -39,7 +39,10 @@ protected static function boot(): void parent::boot(); static::creating(function (Student $student) { if (is_null($student->student_id_number)) { - $student->student_id_number = time().random_int(0, 1000).time(); + $studentIdNumber = $student->gender === 'male' ? 'I' : 'A'; + $studentIdNumber .= now()->toHijri()->isoFormat('YYMM'); + $number = Student::where('student_id_number', 'LIKE', $studentIdNumber . '%')->count() + 1; + $student->student_id_number = $studentIdNumber . str_pad($number, 3, '0',STR_PAD_LEFT); } }); static::deleting(function (Student $student) {