<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ProductAttribute extends Model
{
    public $timestamps = false;

    protected $fillable = [
        'image',
        'count',
        'product_id',
        'attribute_id',
        'choice_id',
        'product_attribute_id',
        'product_attribute_choice_id'
    ];
    public function countSubService()
    {
        $product_attribute = ProductAttribute::where('product_attribute_id',$this->id)->first();
        $count = ProductAttributeChoice::where('product_attribute_id',$product_attribute->id)->sum('count');
        return $count;
    }
    public function subService()
    {
        return $this->hasOne(ProductAttribute::class,'product_attribute_id');
    }
    public function choiceAttr()
    {
        return $this->belongsTo(ProductAttributeChoice::class,'product_attribute_choice_id');
    }
    public function attribute()
    {
        return $this->belongsTo(Attribute::class,'attribute_id')->withTrashed();
    }
    public function choice()
    {
        return $this->belongsTo(Choice::class,'choice_id')->withTrashed();
    }
    public function choices()
    {
        return $this->hasMany(ProductAttributeChoice::class);
    }
}
