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/rentman2019.komma.pro/app/Komma/Products/Models/Product.php
<?php
/**
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

namespace App\Komma\Products\Models;

use App\Komma\Documents\DocumentsTrait;
use App\Komma\Documents\Kms\DocumentableInterface;
use App\Komma\Dynamic\Component\ComponentableInterface;
use App\Komma\Dynamic\Component\ComponentableTrait;
use App\Komma\Kms\Core\Entities\DisplayNameInterface;
use App\Komma\Kms\Core\Entities\DisplayNameTrait;
use App\Komma\Kms\Core\NestedSets\Nodes\AbstractTranslatableTreeModel;
use App\Komma\Languages\Models\Language;
use App\Komma\ProductGroups\Models\ProductGroup;
use App\Komma\Sites\HasSiteInterface;
use App\Komma\Sites\Models\Site;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * Class Page
 *
 * @property int site_id
 * @property int lft
 * @property int rgt
 * @property int tree
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Languages\Models\Language[] $languages
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Sites\Models\Site[] $sites
 * @property-read \App\Komma\Products\Models\ProductTranslation $translation
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Products\Models\ProductTranslation[] $translations
 * @mixin \Eloquent
 * @property int $id
 * @property int $active
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Products\Models\Product whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Products\Models\Product whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Products\Models\Product whereDate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Products\Models\Product whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Products\Models\Product whereUpdatedAt($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Documents\Models\Document[] $documents
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Komma\Documents\Models\Document[] $images
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Products\Models\Product newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Products\Models\Product newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Komma\Products\Models\Product query()
 */
class Product extends AbstractTranslatableTreeModel implements HasSiteInterface, DisplayNameInterface, ComponentableInterface, DocumentableInterface
{
    use DocumentsTrait;
    use DisplayNameTrait;
    use ComponentableTrait;

    protected $table = 'products';

    protected $class = self::class;

    protected $guarded = ['id'];

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

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

    public function productGroup(): BelongsTo
    {
        return $this->belongsTo(ProductGroup::class);
    }

    /**
     * Get the site or sites for this model
     *
     * @return BelongsToMany
     */
    public function site(): BelongsTo
    {
        return $this->belongsTo(Site::class);
    }

    /**
     * Some logic that makes it possible to overrule images by translation images
     *
     * @return \Illuminate\Database\Eloquent\Collection|null
     */
    public function languageImages()
    {
        // Load and check that there are images
        $this->load('images');
        if (! isset($this->images) || $this->images->isEmpty()) {
            return null;
        }

        // First grab the global defined images
        $globalImages = $this->images->where('key', '=', 'Documents-products');

        // Then grab the translation images
        $translationImages = $this->images->where('key', '=', 'Documents-hero_images-'.\App::getLocale());

        // If there are translation images we return those
        if (isset($translationImages) && $translationImages->isNotEmpty()) {
            return $translationImages->values();
        }

        // Else we return the global images
        return $globalImages->values();
    }
}