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
+26
View File
@@ -4,8 +4,10 @@ namespace App\Models\Pertanian;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use App\Models\User;
use App\Models\Pertanian\Petani;
use App\Models\Pertanian\SkorPetani;
class AksesPasar extends Model
{
@@ -38,6 +40,30 @@ class AksesPasar extends Model
protected $casts = [
'saluran_terdahulu' => 'array',
];
protected static function booted(): void
{
static::saved(function (AksesPasar $aksesPasar) {
$petani = Petani::find($aksesPasar->petani_id);
$skor = SkorPetani::where('petani_id')->orderBy('id')->first();
if (!$skor) {
$petaniUid = $petani->uid;
$part = explode('-', $petaniUid);
$skor = SkorPetani::create([
'petani_id' => $petani->id,
'uid' => 'SKR-' . $part[1] . '-' . strtoupper(Str::random(10)),
'created_by' => $petani->created_by,
'updated_by' => $petani->updated_by
]);
}
$skor->update([
'skor_d6_k1_6_pengalaman_menjual_ke_saluran_berbeda' => $aksesPasar->skor_pengalaman,
'skor_d6_k4_1_kesadaran_akses_pasar_premium' => $aksesPasar->skor_kesadaran_premium,
'skor_d6_k5_1_hambatan_mutu' => $aksesPasar->skor_kepercayaan_responden
]);
});
}
public function petani()