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/meuwis/lmbm.be/app/KommaApp/Shop/Products/ProductComposite/ProductComposite.php
<?php

namespace App\KommaApp\Shop\Products\ProductComposite;

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\ProductableInterface;
use App\KommaApp\Shop\Products\ProductGroup\ProductGroup;
use App\KommaApp\Shop\Properties\Models\PropertizableInterface;
use App\KommaApp\Shop\Properties\Models\Property;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Laravel\Scout\Searchable;

/**
 * Represents a basic product that a user can put in a cart
 * 
 * Class ProductComposite
 *
 * @package App
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Categories\Models\Category[] $categories
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Products\ProductGroup\ProductGroup[] $groups
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Images\Models\Image[] $images
 * @property-read \App\KommaApp\Shop\Products\ProductComposite\ProductCompositeTranslation $translation
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Products\ProductComposite\ProductCompositeTranslation[] $translations
 * @mixin \Eloquent
 * @property int $id
 * @property int $active
 * @property string $title
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\ProductComposite\ProductComposite whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\ProductComposite\ProductComposite whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\ProductComposite\ProductComposite whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\ProductComposite\ProductComposite whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\KommaApp\Shop\Products\ProductComposite\ProductComposite whereUpdatedAt($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\KommaApp\Shop\Properties\Models\Property[] $properties
 */
class ProductComposite extends AbstractTranslatableModel implements ProductableInterface, CategorizableInterface, PropertizableInterface, HasImagesInterface
{
    use Searchable;

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

    public function toSearchableArray()
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
        ];
    }

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

    public $thumbnail = false;

    public function groups()
    {
        return $this->belongsToMany(ProductGroup::class);
    }

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

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

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

    /**
     * Returns properties
     * Example key: color
     * Example value of key color: green
     *
     * @return MorphToMany
     */
    function properties(): MorphToMany
    {
        return $this->morphToMany(Property::class, 'propertizable');
    }


    /**
     * Returns the lowest price possible for the composite.
     * Also known as "entry price"
     *
     * @return float
     */
    public function getPrice(): float
    {
        $price = 0;
        $groups = $this->groups()->get();
        $groups->each(function(ProductGroup $group) use (&$price) {
            $price += $group->getPrice();
        });

        return $price;
    }
}