File: D:/HostingSpaces/SBogers10/shop.komma.nl/database/seeds/CommonProperties.php
<?php
namespace App\seeds;
use App\Products\Product\Product;
use App\Products\Product\ProductTranslation;
use App\Properties\Models\PropertizableInterface;
use Komma\KMS\Globalization\Languages\Models\Language;
use App\Properties\Models\Property;
use App\Properties\Models\PropertyKey;
use App\Properties\Models\PropertyKeyTranslation;
use App\Properties\Models\PropertyValue;
use App\Properties\Models\PropertyValueTranslation;
use Illuminate\Database\Seeder;
/**
* Class SampleProperties
*
* Needed for showcasing. Not for testing.
*
* @package App\seeds
*/
class CommonProperties extends Seeder
{
public function run()
{
$propertiesData = [
[
'key' => [
'nl' => 'Kleur',
'en' => 'Color',
'de' => 'Farbe'
],
'values' => [
'nl' => [
'rood',
],
'en' => [
'red',
],
'de' => [
'rot',
]
],
'propertizable' => Product::first(),
],
[
'key' => [
'nl' => 'Afmetingen',
'en' => 'Dimensions',
'de' => 'Maße'
],
'values' => [
'nl' => [
'5x5',
],
'en' => [
'5x5',
],
'de' => [
'5x5',
]
],
'propertizable' => Product::skip(1)->take(1)->first(),
],
];
//Get the iso_639_1 keys from the above array using the keys from the "key" arrays
$iso_639_1_array = array_reduce($propertiesData, function($isoArray, $property) {
return array_merge($isoArray, array_keys($property['key']));
}, []);
$languages = Language::whereIn('iso_2', $iso_639_1_array)->get()->keyBy('iso_2');
foreach($propertiesData as $propertyData)
{
$property = new Property([
'code_name' => '',
'is_required' => false,
'propertizable_id' => Product::first()->id,
'propertizable_type' => Product::class
]);
$key = new PropertyKey();
$key->save();
$property->key()->associate($key);
$property->save();
$value = new PropertyValue();
$value->property()->associate($property);
$value->save();
foreach($languages as $iso_639_1 => $language) {
$keyTranslation = new PropertyKeyTranslation();
$keyTranslation->name = $propertyData['key'][$iso_639_1];
$keyTranslation->language()->associate($languages->get($iso_639_1));
$keyTranslation->translatable()->associate($key);
$keyTranslation->save();
foreach($propertyData['values'][$iso_639_1] as $translation) {
$valueTranslation = new PropertyValueTranslation();
$valueTranslation->name = $translation;
$valueTranslation->translatable()->associate($value);
$valueTranslation->language()->associate($languages->get($iso_639_1));
$valueTranslation->save();
$property->save();
}
}
$property->save();
//Link the property to the propertizable. This then is a free specification
$propertizable = $propertyData['propertizable'];
if(is_a($propertizable, PropertizableInterface::class)) {
/** @var PropertizableInterface $propertizable */
$propertizable->properties()->save($property);
}
}
}
}