[ 'Laptop', 'Stationary', 'Server' ], 'Components' => [ 'Memory', 'Harddrives' => [ 'SSD', 'Mechanical', ], 'Motherboard', 'Graphicscard', 'Soundcard', 'Tools' ], 'Accessories' => [ 'Keyboard', 'Mouse', 'Bags', 'Streaming' ], 'Monitor' => [ 'Gaming monitors', 'Mounting', 'Accessories' ], ]; $this->seedCategoryTree($categories); } /** * @param array $nodes */ private function seedCategoryTree(array $nodes, ?int $parentId = null, string $pathPrefix = ''): void { $sortOrder = 0; foreach ($nodes as $key => $value) { $name = is_int($key) ? (string) $value : (string) $key; $children = is_int($key) ? [] : (is_array($value) ? $value : []); $path = trim($pathPrefix.' '.$name); $slug = Str::slug($path); $category = Category::query()->updateOrCreate( ['slug' => $slug], [ 'parent_id' => $parentId, 'name' => $name, 'description' => null, 'active' => true, 'sort_order' => $sortOrder, ], ); if ($children !== []) { $this->seedCategoryTree($children, (int) $category->id, $path); } $sortOrder++; } } }