112 lines
3.4 KiB
PHP
112 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class Kitten extends Model
|
|
{
|
|
// Mengizinkan semua field kecuali ID untuk diisi secara massal
|
|
protected $guarded = ['id'];
|
|
|
|
/**
|
|
* Casting Data: Memastikan tipe data sinkron antara DB dan PHP.
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'tgl_lahir' => 'date',
|
|
'is_pedigree' => 'boolean',
|
|
'registry' => 'array',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* MODEL EVENTS
|
|
* Membersihkan file storage secara otomatis saat record dihapus.
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::deleting(function ($kitten) {
|
|
// Hapus semua foto pedigree (slot 1-4) jika ada
|
|
for ($i = 1; $i <= 4; $i++) {
|
|
$field = "foto_ped_$i";
|
|
if ($kitten->$field) {
|
|
Storage::disk('public')->delete($kitten->$field);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* RELATIONS */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
* Menghubungkan ke Master Profil Kucing (Patient Table)
|
|
*/
|
|
public function cat(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Cat::class, 'cat_id');
|
|
}
|
|
|
|
/**
|
|
* Menghubungkan ke Event Perkawinan (Breeding Event)
|
|
*/
|
|
public function matting(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Matting::class, 'matting_id');
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* ACCESSORS (UI Helper) */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
* Menghitung berapa banyak dokumen pedigree yang sudah diupload (0-4)
|
|
*/
|
|
protected function pedigreeDocsCount(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function () {
|
|
$count = 0;
|
|
for ($i = 1; $i <= 4; $i++) {
|
|
if ($this->{"foto_ped_$i"}) $count++;
|
|
}
|
|
return $count;
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Badge Status Pedigree untuk Tampilan Tabel
|
|
*/
|
|
protected function pedigreeStatusBadge(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function () {
|
|
if ($this->is_pedigree) {
|
|
return '<span class="px-2 py-0.5 bg-indigo-50 text-indigo-600 rounded text-[9px] font-black uppercase border border-indigo-100 italic">Certificate OK</span>';
|
|
}
|
|
return '<span class="px-2 py-0.5 bg-slate-50 text-slate-400 rounded text-[9px] font-black uppercase border border-slate-100">Non-Ped</span>';
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Mengambil URL Foto Pedigree Slot 1 (Thumbnail)
|
|
*/
|
|
protected function firstPedigreeUrl(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->foto_ped_1
|
|
? asset('storage/' . $this->foto_ped_1)
|
|
: asset('images/default-doc.png')
|
|
);
|
|
}
|
|
} |