petani lahan dan skor

This commit is contained in:
2026-07-10 08:47:48 +07:00
parent 26f6e83328
commit d49bd4953c
16 changed files with 564 additions and 166 deletions
+24
View File
@@ -15,6 +15,8 @@ use App\Models\Wilayah\DesaKelurahan;
use App\Enums\Pendidikan;
use App\Enums\Pernikahan;
use App\Models\Pertanian\SkorPetani;
class Petani extends Model
{
use SoftDeletes;
@@ -78,6 +80,28 @@ class Petani extends Model
return $this->belongsTo(User::class, 'user_id');
}
protected static function booted(): void
{
static::saved(function (Petani $petani) {
$skor = SkorPetani::where('petani_id')->orderBy('id')->first();
if (!$skor) {
$petaniUid = $petani->uid;
$part = explode('-', $petaniUid);
$skor = SkorPetani::create([
'uid' => 'SKR-' . $part[1] . '-' . strtoupper(Str::random(10)),
'created_by' => $petani->created_by,
'updated_by' => $petani->updated_by
]);
}
});
}
public function skor(): HasOne
{
return $this->hasOne(SkorPetani::class, 'petani_id');
}
public function praktikBudidaya(): HasOne
{
return $this->hasOne(PraktikBudidaya::class, 'petani_id')->latestOfMany();