Files
ilycats/app/Models/Food.php
T

108 lines
3.4 KiB
PHP
Raw Normal View History

2026-02-08 15:16:35 +07:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
2026-09-23 12:45:02 +07:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2026-02-08 15:16:35 +07:00
use Illuminate\Support\Str;
class Food extends Model
{
protected $table = 'foods';
2026-09-23 12:45:02 +07:00
// Menggunakan fillable lebih aman untuk tracking kolom apa saja yang masuk ke DB
protected $fillable = [
'nama_makanan',
'foto_produk',
'slug',
'qty',
'unit',
'category_id',
'kategori',
'expired_at'
];
/**
* Casting tipe data agar terbaca sebagai objek oleh Carbon
*/
protected function casts(): array
{
return [
'expired_at' => 'date',
'qty' => 'float',
'created_at' => 'datetime',
];
}
2026-02-08 15:16:35 +07:00
/**
* Relasi ke model Category
*/
2026-09-23 12:45:02 +07:00
public function category(): BelongsTo
2026-02-08 15:16:35 +07:00
{
return $this->belongsTo(Category::class, 'category_id');
}
/**
* Booting model events
*/
protected static function booted()
{
static::saving(function ($food) {
// 1. OTOMATIS GENERATE SLUG
if ($food->isDirty('nama_makanan') || empty($food->slug)) {
2026-09-23 12:45:02 +07:00
$food->slug = Str::slug($food->nama_makanan) . '-' . Str::lower(Str::random(5));
2026-02-08 15:16:35 +07:00
}
// 2. AUTO-CATEGORIZATION (SMART LOGIC)
2026-09-23 12:45:02 +07:00
// Jalankan hanya jika category_id belum diisi manual atau nama makanan berubah
if (empty($food->category_id) || $food->isDirty('nama_makanan')) {
2026-02-08 15:16:35 +07:00
$productName = strtolower($food->nama_makanan);
$categories = Category::whereNotNull('keywords')->get();
$found = false;
foreach ($categories as $cat) {
$keywordsArray = explode(',', strtolower($cat->keywords));
foreach ($keywordsArray as $keyword) {
$cleanKeyword = trim($keyword);
if (!empty($cleanKeyword) && str_contains($productName, $cleanKeyword)) {
$food->category_id = $cat->id;
$food->kategori = $cat->name;
$found = true;
2026-09-23 12:45:02 +07:00
break 2; // Keluar dari kedua loop sekaligus
2026-02-08 15:16:35 +07:00
}
}
}
// 3. FALLBACK LOGIC
2026-09-23 12:45:02 +07:00
if (!$found && empty($food->category_id)) {
2026-02-08 15:16:35 +07:00
$default = Category::where('name', 'Dry Food')
2026-09-23 12:45:02 +07:00
->orWhere('name', 'Umum')
->first();
2026-02-08 15:16:35 +07:00
if ($default) {
$food->category_id = $default->id;
$food->kategori = $default->name;
} else {
2026-09-23 12:45:02 +07:00
$food->kategori = $food->kategori ?: 'Umum';
2026-02-08 15:16:35 +07:00
}
}
}
2026-09-23 12:45:02 +07:00
// 4. SINKRONISASI TEKS KATEGORI
// Jika category_id diisi manual di form, pastikan kolom 'kategori' (string) juga terupdate
if ($food->isDirty('category_id') && !empty($food->category_id)) {
$food->kategori = $food->category?->name ?? $food->kategori;
}
2026-02-08 15:16:35 +07:00
});
}
2026-09-23 12:45:02 +07:00
/**
* Accessor: Cek Status Expired
*/
public function getIsExpiredAttribute(): bool
{
return $this->expired_at ? $this->expired_at->isPast() : false;
}
2026-02-08 15:16:35 +07:00
}