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/database/seeds/TestCategories.php
<?php

namespace App\seeds;


use Illuminate\Support\Str;
use Komma\KMS\Globalization\Languages\Models\Language;
use App\Categories\Models\Category;
use App\Categories\Models\CategoryTranslation;
use Illuminate\Database\Seeder;

class TestCategories extends Seeder
{
    public function run()
    {
        $root = new Category(['active' => '0', 'site_id' => null, 'code_name' => 'root']); //Null for the default site
        $root->makeRoot();
        $root->save();

        $categories = [
            [
                'code_name' => 'garden_diy',
                'nl' => "Tuin & Klussen",
                'de' => "Garten & DIY",
                'en' => "Garden & DIY",
                'children' => [
                    [
                        'code_name' => 'handtools',
                        'nl' => 'Handgereedschap',
                        'de' => 'Handwerkzeuge',
                        'en' => 'Handtools'
                    ],
                    [
                        'code_name' => 'barbecues',
                        'nl' => 'Barbecues',
                        'de' => 'Barbecues',
                        'en' => 'Barbecues'
                    ],
                ],
            ],
            [
                'code_name' => 'photography',
                'nl' => "Fotografie",
                'de' => "Fotografie",
                'en' => "Photography",
                'children' => [
                    [
                        'code_name' => 'studio_lightning',
                        'nl' => 'Studio verlichting',
                        'de' => 'Studio beleuchtung',
                        'en' => 'Studio Lighting'
                    ],
                ],
            ]
        ];


        foreach ($categories as $mainCategoryData) {
            $mainCategory = $this->createCategory($mainCategoryData, $root);
        }
    }

    /**
     * Create a category from the array data you pass it.
     *
     * @param $categoryData
     * @return Category
     */
    private function createCategory($categoryData, $parent)
    {
        $category = new Category();

        if(!isset($categoryData['code_name'])) dd($categoryData);
        $category->code_name = $categoryData['code_name'];
        $category->active = 1;
        $category->makeLastChildOf($parent); //Also saves

        foreach ($categoryData as $key => $value) {
            switch ($key) {
                case 'code_name':
                    break;
                case 'children':
                    foreach ($value as $childCategoryData) {
                        $this->createCategory($childCategoryData, $category);
                    }
                    break;
                default:
                    $language = Language::where('iso_2', '=', $key)->first();
                    if (!$language) {
                        break;
                    }
                    $translation = new CategoryTranslation();
                    $translation->language()->associate($language);
                    $translation->translatable()->associate($category);
                    $translation->slug = Str::slug($value);
                    $translation->name = $value;
                    $translation->meta_title = $value;
                    $translation->save();
            }
        }

        return $category;
    }
}