File: D:/HostingSpaces/SBogers85/equichecker.com/app/KommaApp/Core/Models/EavModel.php
<?php
namespace KommaApp\Core\Models;
use Illuminate\Database\Eloquent\Model;
class EavModel extends Model
{
protected $keyedRelatedAttributes = null;
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
}
public function getValue($attribute_type, $siteId = null, $languageId = null)
{
if ($this->keyedRelatedAttributes == null) $this->loadRelatedAttributes();
if (!isset($this->keyedRelatedAttributes[$attribute_type])) return null;
$at = $this->keyedRelatedAttributes[$attribute_type];
$values = $at->values
->where('site_id', $siteId)
->filter(function ($values) use ($languageId) {
//Filter where language is null or language is the current language
return $values->language_id == $languageId || $values->language_id == null;
});
$values = $values->fetch('value_' . $at->data_type);
if ($values->count() == 1) {
return $values->first();
}
return $values;
}
private function loadRelatedAttributes()
{
$this->keyedRelatedAttributes = $this->relatedAttributes->keyBy('attribute_type');
}
}