File: D:/HostingSpaces/Eurotools/euro-tools.nl/app/KommaApp/Shop/Categories/Models/Category.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Shop\Categories\Models;
use App\KommaApp\Documents\Kms\DocumentableInterface;
use App\KommaApp\Documents\Models\Document;
use App\KommaApp\Images\Models\Image;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Kms\Core\HasImagesInterface;
use App\KommaApp\Kms\Core\NestedSets\Nodes\TranslatableEloquentNode;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Shop\Products\Product\Product;
use App\KommaApp\Shop\Products\ProductComposite\ProductComposite;
use App\KommaApp\Shop\Products\ProductGroup\ProductGroup;
use App\KommaApp\Sites\Models\Site;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
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;
/**
* Class Page
*
* @package App\KommaApp\Shop\Categories\Models
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Images\Models\Image[] $images
* @property \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Documents\Models\Document[] $documents
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Languages\Models\Language[] $languages
* @property-read \App\KommaApp\Sites\Models\Site $site
* @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Categories\Models\CategoryTranslation[] $translations
* @mixin \Eloquent
* @property \App\KommaApp\Shop\Categories\Models\CategoryTranslation $translation
* @property int $id
* @property string $code_name
* @property int $active
* @property int|null $lft
* @property int|null $rgt
* @property int|null $tree
* @property int $site_id
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Categories\Models\Category whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Categories\Models\Category whereCodeName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Categories\Models\Category whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Categories\Models\Category whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Categories\Models\Category whereLft($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Categories\Models\Category whereRgt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Categories\Models\Category whereSiteId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Categories\Models\Category whereTree($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Categories\Models\Category whereUpdatedAt($value)
*/
class Category extends TranslatableEloquentNode implements DocumentableInterface, HasImagesInterface
{
use Searchable;
public $asYouType = true;
public function searchableAs()
{
return 'category_index';
}
public function toSearchableArray()
{
return [
'id' => $this->id,
'site_id' => $this->site_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;
/*
* Transient properties on Eloquent models
* These are not saved to database.
*/
public $thumbnail = false;
// public $parent_id;
protected $fillable = ['active', 'site_id', 'lft', 'rgt', 'tree', 'code_name'];
public function site(): BelongsTo
{
return $this->belongsTo(Site::class);
}
/**
* 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 images from the current user
*
* @return \Illuminate\Database\Eloquent\Relations\hasMany
*/
public function images(): HasMany
{
/**
*
* On the Image model is an MorphTo relation
* By using a hasMany relation:
* where the imageable_type is filled in with the KmsClass
* And the imageable_id is set as the foreign_id,
* we can collect the images of the given model directly.
*
*/
return $this->hasMany(Image::class, 'imageable_id')
->where('imageable_type', '=', $this->class);
}
/**
* Get the documents from the current model
*
* @return \Illuminate\Database\Eloquent\Relations\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, 'product_categorizable', 'product_categorizables');
}
/**
* Get a collection the products related to this category
* //TODO: This method is used by blades directly. This method should not be used. But the regular products one in the frontend product controller, constraining that relation with the eager loads. This violates DRY
*/
public function getProducts(): MorphToMany
{
return $this->morphedByMany(Product::class, 'product_categorizable', 'product_categorizables')
->where('active', '=', true)
->with('translation', 'documents')
->has('translation');
}
/**
* Get the products related to this category
*/
public function product_groups(): MorphToMany
{
return $this->morphedByMany(ProductGroup::class, 'product_categorizable', 'product_categorizables');
}
/**
* Get the products related to this category
*/
public function product_composites(): MorphToMany
{
return $this->morphedByMany(ProductComposite::class, 'product_categorizable', 'product_categorizables');
}
public function items(): Collection
{
$products = $this->products()->get();
$productGroups = $this->product_groups()->get();
$productComposites = $this->product_composites()->get();
return $products->concat($productGroups)->concat($productComposites);
}
public function __get($key)
{
if ($key == "title") {
if ($this->translation) {
return $this->translation->name;
}
}
if ($key == "parent_id") {
if ($this::find($this->id)) {
return $this->getParentId();
}
}
return parent::__get($key);
}
}