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/Lacom/lacom.nl/app/KommaApp/Shop/Products/ProductGroup/ProductGroup.php
<?php

namespace App\KommaApp\Shop\Products\ProductGroup;

use App\KommaApp\Images\Models\Image;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Kms\Core\HasImagesInterface;
use App\KommaApp\Shop\Categories\Kms\CategorizableInterface;
use App\KommaApp\Shop\Categories\Models\Category;
use App\KommaApp\Shop\Products\Product\Product;
use App\KommaApp\Shop\Products\ProductComposite\ProductComposite;
use App\KommaApp\Shop\Products\ProductGroupBehaviour\ProductGroupBehaviour;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

/**
 * Groups products together (not like a product category)
 * to link behaviour.
 *
 * Class ProductGroup
 * @package App
 * @property string title
 */
class ProductGroup extends AbstractTranslatableModel implements CategorizableInterface, HasImagesInterface
{
    /*
    * Transient properties on Eloquent models
    * These are not saved to database.
    */
    public $thumbnail = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['title'];

    public function productGroupBehaviour()
    {
        return $this->belongsTo(ProductGroupBehaviour::class);
    }

    public function products()
    {
        return $this->belongsToMany(Product::class);
    }

    public function composites()
    {
        return $this->belongsToMany(ProductComposite::class);
    }

    /**
     * We belong to many categories
     *
     * @return MorphToMany
     */
    public function categories(): MorphToMany
    {
        return $this->morphToMany(Category::class, 'product_categorizable');
    }

    /**
     * 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', '=', ProductGroup::class);
    }

    public function translations(): HasMany
    {
        return $this->hasMany(ProductGroupTranslation::class);
    }
}