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()];
});
}
}