<?php

namespace App\Models;

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

class MenuTerlarisView extends Model
{
    use HasFactory;

    protected $table = 'menu_terlaris_view';

    // full path to reach image from storage folder and check if image exists or not
    public function getGambarProdukAttribute($value)
    {
        $storage = storage_path('app/public/produk/' . $this->kategori_produk_id . '/' . $value);
        if (file_exists($storage)) {
            return asset('storage/produk/' . $this->kategori_produk_id . '/' . $value);
        } else {
            return asset('assets/images/menu_image.jpeg');
        }
    }

    public function kategori_produk()
    {
        return $this->belongsTo(KategoriProduk::class, 'kategori_produk_id');
    }

    public function kelompokKategori()
    {
        return $this->belongsToMany(KelompokKategori::class, KelompokKategoriPivot::class, 'produk_id', 'kelompok_kategori_id');
    }

    public function kelompokKategoriPivot()
    {
        return $this->hasMany(KelompokKategoriPivot::class, 'produk_id');
    }
}