<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SubKriteria extends Model
{
    use HasFactory;
    protected $primaryKey = 'id';
    protected $table = 'data_sub_kriterias';
    protected $fillable = ['kriteria_id', 'nama_sub_kriteria', 'bobot'];

    public function kriterias()
    {
        return $this->belongsTo(Kriteria::class, 'kriteria_id');
    }

    public function penilaians()
    {
        return $this->hasMany(Penilaian::class);
    }
    protected static function boot()
    {
        parent::boot();

        static::saving(function ($model) {
            if ($model->bobot < 1 || $model->bobot > 4) {
                throw new \Exception('Nilai bobot harus berada di antara 1 dan 4.');
            }
        });
    }
}

