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/blije-gasten.komma.pro/app/Komma/Shop/seeds/CategoriesSeeder.php
<?php

namespace App\Komma\Shop\seeds;


use App\Komma\Globalization\Languages\Models\Language;
use App\Komma\Shop\Categories\Models\Category;
use App\Komma\Shop\Categories\Models\CategoryTranslation;
use Illuminate\Database\Seeder;

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

        $categories = [
            [
                'code_name' => 'glass_porcelain',
                'nl' => "Glas en porselein",
            ],
            [
                'code_name' => 'cookers',
                'nl' => "Bak en kooktoestellen",
            ],
            [
                'code_name' => 'furniture',
                'nl' => "Meubelair",
            ],
            [
                'code_name' => 'heaters',
                'nl' => "Verwarming",
            ],
            [
                'code_name' => 'coolers',
                'nl' => "Koeling",
            ],
            [
                'code_name' => 'bouncy_castle',
                'nl' => "Springkastelen",
            ],
            [
                'code_name' => 'cutlery',
                'nl' => "Bestek",
            ],
            [
                'code_name' => 'hall_material',
                'nl' => "Zaalmateriaal",
            ],
            [
                'code_name' => 'kitchen_equipment',
                'nl' => "Keukenmateriaal",
            ],
        ];


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

        $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;
    }
}