File: D:/HostingSpaces/centrum8a/centrum8a.com/app/KommaApp/Shop/Properties/Models/Property.php
<?php
namespace App\KommaApp\Shop\Properties\Models;
use App\KommaApp\Shop\Products\Product\Product;
use App\KommaApp\Shop\Products\ProductComposite\ProductComposite;
use App\KommaApp\Shop\Products\ProductGroup\ProductGroup;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
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\KommaApp\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\KommaApp\Shop\Properties\Models\PropertyKey $key
* @property-read \App\KommaApp\Shop\Properties\Models\PropertyValue $value
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Properties\Models\Property whereCodeName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Properties\Models\Property whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Properties\Models\Property whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Properties\Models\Property wherePropertyKeyId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Properties\Models\Property wherePropertyValueId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Properties\Models\Property whereUpdatedAt($value)
* @mixin \Eloquent
*/
class Property extends Model
{
/*
* Transient properties on Eloquent models
* These are not saved to database.
*/
public $thumbnail = false;
function key():BelongsTo
{
return $this->BelongsTo(PropertyKey::class, 'property_key_id');
}
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');
}
public function __get($key)
{
if($key == "title") {
$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 parent::__get($key);
}
}