File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Shop/seeds/SampleProducts.php
<?php
namespace App\Komma\Shop\seeds;
use App\Komma\Globalization\Languages\Models\Language;
use App\Komma\Shop\Products\Product\Product;
use App\Komma\Shop\Products\Product\ProductTranslation;
use App\Komma\Shop\Products\ProductComposite\ProductComposite;
use App\Komma\Shop\Products\ProductComposite\ProductCompositeTranslation;
use App\Komma\Shop\Products\ProductGroup\ProductGroup;
use App\Komma\Shop\Products\ProductGroup\ProductGroupTranslation;
use App\Komma\Shop\Products\ProductGroupBehaviour\ProductGroupBehaviour;
use Illuminate\Database\Seeder;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class SampleProducts extends Seeder
{
public function run()
{
// for($i = 0; $i < 50; $i++) {
// $model = factory(Product::class);
// $model->create();
// }
$productsData = [
[
'attributes' => [
'active' => '1',
'Price' => '799',
'custom_vatrate' => '9',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Basic - Attachment materials',
],
],
'nl' => [
'attributes' => [
'name' => 'Basis - Bevestigings materialen',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '2499',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Cassette - Plastic - Aluminum',
],
],
'nl' => [
'attributes' => [
'name' => 'Casette - Plastic - Aluminium',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '1999',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Cassette - Plastic - White',
],
],
'nl' => [
'attributes' => [
'name' => 'Cassette - Plastic - Wit',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '1999',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Cassette - Plastic - Beige',
],
],
'nl' => [
'attributes' => [
'name' => 'Cassette - Plastic - Beige',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '299',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Bottom bar - Plastic - Aluminium',
],
],
'nl' => [
'attributes' => [
'name' => 'Onderste balk - Plastic - Aluminium',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '299',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Bottom bar - White - Aluminium',
],
],
'nl' => [
'attributes' => [
'name' => 'Onderste balk - Wit - Aluminium',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '299',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Bottom bar - Beige - Aluminium',
],
],
'nl' => [
'attributes' => [
'name' => 'Onderste balk - Beige - Aluminium',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '299',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Control left - Chain',
],
],
],
'nl' => [
'attributes' => [
'name' => 'Bediening links - Ketting',
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '299',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Control right - Chain',
],
],
'nl' => [
'attributes' => [
'name' => 'Bediening rechts - Ketting',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '794',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric - White',
],
],
'nl' => [
'attributes' => [
'name' => 'Stof - Wit',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '794',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric - White',
],
],
'nl' => [
'attributes' => [
'name' => 'Stof - Wit',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '794',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric - Blackout dark',
],
],
'nl' => [
'attributes' => [
'name' => 'Stof - Verduisterend zwart',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '794',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric - Cream',
],
],
'nl' => [
'attributes' => [
'name' => 'Stof - Crème',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '794',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric - Beige',
],
],
'nl' => [
'attributes' => [
'name' => 'Stof - Gebroken',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '794',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric - Grey',
],
],
'nl' => [
'attributes' => [
'name' => 'Stof - Grijs',
],
],
],
],
[
'attributes' => [
'active' => '1',
'Price' => '100',
'custom_vatrate' => '9',
'stock_keeping_unit' => 'EAN'.mt_rand(1000000, 9999999),
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Acme Food',
],
],
],
],
];
$productGroupsData = [
[
'attributes' => [
'active' => '1',
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Roller blind basic set',
],
],
],
],
[
'attributes' => [
'active' => '1',
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Cassette',
],
],
],
],
[
'attributes' => [
'active' => '1',
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Control',
],
],
],
],
[
'attributes' => [
'active' => '1',
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Bottom bar',
],
],
],
],
[
'attributes' => [
'active' => '1',
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric',
],
],
],
],
];
// $orBehaviour = ProductGroupBehaviour::where('name', 'or')->first();
$andBehaviour = ProductGroupBehaviour::where('name', 'and')->first();
// $xorBehaviour = ProductGroupBehaviour::where('name', 'xor')->first();
$products = [];
foreach ($productsData as $productData) {
$products[] = $this->createProduct($productData);
}
$productGroups = [];
foreach ($productGroupsData as $productGroupData) {
$productGroups[] = $this->createProductGroup($productGroupData, $andBehaviour, $products);
}
$productComposite = new ProductComposite(['active' => '1']);
$productComposite->save();
$translation = new ProductCompositeTranslation(['name' => 'Roller blind', 'slug' => Str::slug('Roller blind')]);
$translation->language()->associate(Language::where('iso_2', '=', 'en')->first());
$translation->translatable()->associate($productComposite);
$translation->save();
foreach ($productGroups as $productGroup) {
$productComposite->groups()->attach($productGroup);
}
}
private function createProduct(array $productData): Product
{
$product = new Product($productData['attributes']);
$product->save();
foreach ($productData['translations'] as $iso_2 => $languageData) {
$language = Language::where('iso_2', '=', $iso_2)->first();
if (! $language) {
break;
}
$translation = new ProductTranslation();
$translation->language()->associate($language);
$translation->translatable()->associate($product);
$translation->fill(
$languageData['attributes']
);
$translation->slug = Str::slug($translation->name);
$translation->save();
}
return $product;
}
private function createProductGroup(array $productGroupData, ProductGroupBehaviour $productGroupBehaviour, array $products): ProductGroup
{
$productGroup = new ProductGroup($productGroupData['attributes']);
$productGroup->productGroupBehaviour()->associate($productGroupBehaviour);
$productGroup->save();
foreach ($productGroupData['translations'] as $iso_2 => $languageData) {
$language = Language::where('iso_2', '=', $iso_2)->first();
if (! $language) {
break;
}
$translation = new ProductGroupTranslation();
$translation->language()->associate($language);
$translation->translatable()->associate($productGroup);
$translation->slug = Str::slug($translation->name);
$translation->fill(
$languageData['attributes']
);
$translation->save();
}
$randomProducts = Arr::random($products, mt_rand(1, 3));
$productGroup->products()->saveMany($randomProducts);
return $productGroup;
}
}