HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
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');

    }

}