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/SBogers10/shop.komma.nl/app/Categories/Models/Category.php
<?php
namespace App\Categories\Models;

use App\Properties\Models\PropertyKey;
use Komma\KMS\Core\AbstractTranslatableModel;
use Komma\KMS\Core\Tree\NestedSets\Nodes\TreeModelInterface;
use Komma\KMS\Core\Tree\NestedSets\Nodes\TreeModelLogicTrait;
use Komma\KMS\Documents\Kms\DocumentableInterface;
use Komma\KMS\Documents\Models\Document;
use Komma\KMS\Core\Entities\DisplayNameInterface;
use Komma\KMS\Core\Entities\DisplayNameTrait;
use Komma\KMS\Globalization\Languages\Models\Language;
use App\Products\Product\Product;
use App\Products\ProductComposite\ProductComposite;
use App\Products\ProductGroup\ProductGroup;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Laravel\Scout\Searchable;

/**
 * App\Categories\Models\Category
 *
 * @property int $id
 * @property string $code_name
 * @property int $active
 * @property int|null $site_id
 * @property int|null $lft
 * @property int|null $rgt
 * @property int|null $tree
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|Document[] $documents
 * @property-read int|null $documents_count
 * @property-read \Illuminate\Database\Eloquent\Collection|Language[] $languages
 * @property-read int|null $languages_count
 * @property-read \Illuminate\Database\Eloquent\Collection|ProductComposite[] $product_composites
 * @property-read int|null $product_composites_count
 * @property-read \Illuminate\Database\Eloquent\Collection|ProductGroup[] $product_groups
 * @property-read int|null $product_groups_count
 * @property-read \Illuminate\Database\Eloquent\Collection|Product[] $products
 * @property-read int|null $products_count
 * @property-read \Illuminate\Database\Eloquent\Collection|PropertyKey[] $requiredPropertyKeys
 * @property-read int|null $required_property_keys_count
 * @property-read \App\Categories\Models\CategoryTranslation|null $translation
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Categories\Models\CategoryTranslation[] $translations
 * @property-read int|null $translations_count
 * @method static \Illuminate\Database\Eloquent\Builder|Category newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Category newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Category query()
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereCodeName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereLft($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereRgt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereSiteId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereTree($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Category extends AbstractTranslatableModel implements DocumentableInterface, DisplayNameInterface, TreeModelInterface
{
    use Searchable;
    use DisplayNameTrait;
    use TreeModelLogicTrait;

    public function searchableAs()
    {
        return 'category_index';
    }

    public function toSearchableArray()
    {
        return [
          'id' => $this->id,
          'active' => $this->active,
          'code_name' => $this->code_name,
          'lft' => $this->lft,
          'rgt' => $this->rgt,
          'tree' => $this->tree
        ];
    }

    protected $table = 'product_categories';
    protected $class = Category::class;


    protected $fillable = ['active', 'lft', 'rgt', 'tree', 'code_name'];

    /**
     * Gets the translation models for this model
     *
     * @return HasMany that resolves to AbstractTranslationModel instances
     */
    public function translations(): HasMany
    {
        return $this->hasMany(CategoryTranslation::class);
    }

    public function languages(): BelongsToMany
    {
        return $this->belongsToMany(Language::class)
            ->withPivot('slug', 'name', 'description')
            ->withTimestamps();
    }

    /**
     * Get the documents from the current model
     *
     * @return MorphMany
     */
    public function documents():MorphMany
    {
        return $this->morphMany(Document::class, 'documentable');
    }

    /**
     * Get the products related to this category
     */
    public function products():MorphToMany
    {
        return $this->morphedByMany(Product::class, 'categorizable', 'categorizables');
    }

    /**
     * Get the products related to this category
     */
    public function product_groups():MorphToMany
    {
        return $this->morphedByMany(ProductGroup::class, 'categorizable', 'categorizables');
    }

    /**
     * Get the products related to this category
     */
    public function product_composites():MorphToMany
    {
        return $this->morphedByMany(ProductComposite::class, 'categorizable', 'categorizables');
    }

    /**
     * @return BelongsToMany
     */
    public function requiredPropertyKeys(): BelongsToMany {
        return $this->belongsToMany(PropertyKey::class);
    }

    public function getDisplayName():string {
        return $this->translation->name ?? '';
    }

    public function __get($key)
    {
        if($key == "title") {
            if($this->translation) {
                return $this->translation->name;
            } else if($this->code_name != '') {
                return $this->code_name;
            }
        }

        return parent::__get($key);
    }
}