sarga/packages/Webkul/Product/src/Models/Product.php

128 lines
3.1 KiB
PHP

<?php
namespace Webkul\Product\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Attribute\Models\AttributeFamily;
use Webkul\Category\Models\Category;
use Webkul\Attribute\Models\Attribute;
use Webkul\Product\Models\ProductAttributeValue;
use Webkul\Inventory\Models\InventorySource;
class Product extends Model
{
protected $fillable = ['type', 'attribute_family_id', 'sku', 'parent_id'];
protected $with = ['attribute_values', 'varients'];
/**
* @var array
*/
protected $attributeTypeFields = [
'text' => 'text_value',
'textarea' => 'text_value',
'price' => 'float_value',
'boolean' => 'boolean_value',
'select' => 'integer_value',
'multiselect' => 'text_value',
'datetime' => 'datetime_time',
'date' => 'date_value',
];
/**
* Get the product attribute family that owns the product.
*/
public function attribute_family()
{
return $this->belongsTo(AttributeFamily::class);
}
/**
* Get the product attribute values that owns the product.
*/
public function attribute_values()
{
return $this->hasMany(ProductAttributeValue::class);
}
/**
* Get the product varients that owns the product.
*/
public function varients()
{
return $this->hasMany(self::class, 'parent_id');
}
/**
* The categories that belong to the product.
*/
public function categories()
{
return $this->belongsToMany(Category::class, 'product_categories');
}
/**
* The inventories that belong to the product.
*/
public function inventories()
{
return $this->belongsToMany(InventorySource::class, 'product_inventories');
}
/**
* The super attributes that belong to the product.
*/
public function super_attributes()
{
return $this->belongsToMany(Attribute::class, 'product_super_attributes');
}
/**
* The related products that belong to the product.
*/
public function related_products()
{
return $this->belongsToMany(self::class, 'product_relations');
}
/**
* The up sells that belong to the product.
*/
public function up_sells()
{
return $this->belongsToMany(self::class, 'product_up_sells');
}
/**
* The cross sells that belong to the product.
*/
public function cross_sells()
{
return $this->belongsToMany(self::class, 'product_cross_sells');
}
/**
* @return array
*/
public function attributesToArray()
{
$attributes = parent::attributesToArray();
$hiddenAttributes = $this->getHidden();
foreach ($this->attribute_values as $attributeValue) {
$attribute = $attributeValue->attribute;
if (in_array($attribute->code, $hiddenAttributes)) {
continue;
}
if ($value = $attributeValue[$this->attributeTypeFields[$attribute->type]]) {
$attributes[$attribute->code] = $value;
}
}
return $attributes;
}
}