*/ protected $fillable = [ 'id_kelompok', 'nama_kelompok', 'deskripsi', 'urutan', 'is_active', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'is_active' => 'boolean', 'urutan' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * Boot method untuk auto-generate id_kelompok */ protected static function boot() { parent::boot(); static::creating(function ($model) { if (empty($model->id_kelompok)) { $last = self::orderBy('id', 'desc')->first(); $num = $last ? intval(substr($last->id_kelompok, 3)) + 1 : 1; $model->id_kelompok = 'KEL' . str_pad($num, 3, '0', STR_PAD_LEFT); } }); } /** * Relasi: Kelompok memiliki banyak kelas (One to Many) * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function kelas() { return $this->hasMany(Kelas::class, 'id_kelompok', 'id_kelompok'); } /** * Scope: Filter kelompok yang aktif * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeActive($query) { return $query->where('is_active', true); } /** * Scope: Order by urutan * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeOrdered($query) { return $query->orderBy('urutan', 'asc'); } /** * Accessor: Total kelas dalam kelompok * * @return int */ public function getTotalKelasAttribute() { return $this->kelas()->count(); } /** * Accessor: Total kelas aktif dalam kelompok * * @return int */ public function getTotalKelasAktifAttribute() { return $this->kelas()->where('is_active', true)->count(); } }