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/blijegasten/blijegasten.be/app/Komma/Shop/Properties/Models/Property.php
<?php


namespace App\Komma\Shop\Properties\Models;

use App\Komma\Kms\Core\Attributes\Models\Traits\HasThumbnailInterface;
use App\Komma\Kms\Core\Attributes\Models\Traits\HasThumbnailTrait;
use App\Komma\Kms\Core\Entities\DisplayNameInterface;
use App\Komma\Kms\Core\Entities\DisplayNameTrait;
use App\Komma\Shop\Products\Product\Product;
use App\Komma\Shop\Products\ProductComposite\ProductComposite;
use App\Komma\Shop\Products\ProductGroup\ProductGroup;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

/**
 * Class PropertyableKeyValues
 * 
 * Represents a key value pair or ... a property. For example: "color" (key), "green" (value), "blue" (value)
 * Can be used for example to retrieve all "color" values.
 *
 * @property-read int $id
 * @property string $code_name
 * @package App\Komma\Shop\Properties\Models
 * @property int $property_key_id
 * @property int $property_value_id
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @property-read \App\Komma\Shop\Properties\Models\PropertyKey $key
 * @property-read \App\Komma\Shop\Properties\Models\PropertyValue $value
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Properties\Models\Property whereCodeName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Properties\Models\Property whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Properties\Models\Property whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Properties\Models\Property wherePropertyKeyId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Properties\Models\Property wherePropertyValueId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Properties\Models\Property whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Products\ProductComposite\ProductComposite[] $product_composites
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Products\ProductGroup\ProductGroup[] $product_groups
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Shop\Products\Product\Product[] $products
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Properties\Models\Property newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Properties\Models\Property newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Shop\Properties\Models\Property query()
 */
class Property extends Model implements HasThumbnailInterface, DisplayNameInterface
{
    use HasThumbnailTrait;

    /**
     * The key of the property. E.g. the name. The key has multiple translations like "color", "kleur"
     *
     * @see PropertyKey
     * @return BelongsTo
     */
    function key():BelongsTo
    {
        return $this->BelongsTo(PropertyKey::class, 'property_key_id');
    }

    /**
     * The value of the property. E.g. The value of the key. The value has multiple translations like "green", "groen"
     *
     * @see PropertyValue
     * @return BelongsTo
     */
    function value():BelongsTo
    {
        return $this->BelongsTo(PropertyValue::class, 'property_value_id');
    }

    function products(): MorphToMany
    {
        return $this->morphedByMany(Product::class, 'propertizable');
    }

    function product_groups(): MorphToMany
    {
        return $this->morphedByMany(ProductGroup::class, 'propertizable');
    }

    function product_composites(): MorphToMany
    {
        return $this->morphedByMany(ProductComposite::class, 'propertizable');
    }

    /**
     * Get the name of this model by model or translation model
     *
     * @return null|string
     */
    public function getDisplayName(): ?string
    {
        $propertyKey = ($this->relationLoaded('key')) ? $this->key : $this->key()->first();
        $propertyValue = ($this->relationLoaded('value')) ? $this->value : $this->value()->first();

        if($propertyKey && $propertyValue) {
            $translationKey = ($propertyKey->relationLoaded('translations')) ? $propertyKey->translations->first() : $propertyKey->translations()->first();
            $translationValue = ($propertyValue->relationLoaded('translations')) ? $propertyValue->translations->first() : $propertyValue->translations()->first();

            if($translationKey && $translationValue) return $translationKey->value.' - '.$translationValue->value;
        }

        return '';
    }

    /**
     * Get the name for the sidebar
     *
     * @return string
     */
    public function getSidebarName(): string
    {
        return $this->getDisplayName() ?? '';
    }
}