File: D:/HostingSpaces/ASmits/kemi.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);
}
}