<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Product extends Model
{
    protected $table   = 'product';
    protected $guarded = [];
    public function cats()
    {
        return $this->belongsTo(ProductCat::class, 'cat_id');
    }
    protected static function boot()
    {
        parent::boot();

        static::creating(function ($product) {
            if (empty($product->slug) && ! empty($product->name_en)) {
                $product->slug = Str::slug($product->name_en);
            }
        });

        static::updating(function ($product) {
            if (empty($product->slug) && ! empty($product->name_en)) {
                $product->slug = Str::slug($product->name_en);
            }
        });
    }

    public function category()
    {
        return $this->belongsTo(ProductCat::class, 'cat_id');
    }
}
