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/shop.komma.nl/app/Properties/Models/PropertizableTrait.php
<?php declare(strict_types=1);


namespace App\Properties\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Collection;

/**
 * Class PropertizableTrait
 *
 * @mixin Model
 * @package App\Properties\Models
 */
trait PropertizableTrait
{
    /**
     * Returns properties
     * Example key: color
     * Example value of key color: green
     *
     * @return MorphMany
     */
    function properties(): MorphMany
    {
        return $this->morphMany(Property::class, 'propertizable');
    }

    function displayProperties(): Collection {
        return $this->properties->filter((function(Property $property) {
            return count($property->values) > 0;
        }))->mapWithKeys(function(Property $property) {
            return [$property->key->getDisplayName() => $property->values[0]->getDisplayName()];
        });
    }
}