*/ public function toArray(Request $request): array { $heroConfig = $this->hero_config; if (is_array($heroConfig)) { $heroConfig['background_image'] = $this->heroBgImage?->getTemporaryUrl(1440); unset($heroConfig['background_image_id']); } return [ 'id' => $this->id, 'codigo' => $this->codigo, 'nombre' => $this->nombre, 'dominio' => $this->dominio, 'primary_color' => $this->primary_color, 'secondary_color' => $this->secondary_color, 'danger_color' => $this->danger_color, 'success_color' => $this->success_color, 'header_bg_color' => $this->header_bg_color, 'footer_bg_color' => $this->footer_bg_color, // 1 day 'header_logo' => $this->headerLogo?->getTemporaryUrl(1440), 'footer_logo' => $this->footerLogo?->getTemporaryUrl(1440), 'hero_config' => $heroConfig, 'event_config' => $this->event_config, 'search_product_layout' => $this->search_product_layout->value, 'search_group_layout' => $this->search_group_layout->value, 'search_items_per_page' => $this->search_items_per_page, 'main_carousel_images' => $this->whenLoaded( 'mainCarouselImages', fn () => $this->mainCarouselImages ->map(fn ($attachment) => $attachment->getTemporaryUrl(1440)) ->values() ), 'social_media' => $this->whenLoaded( 'socialMedia', fn () => $this->socialMedia ->map(fn ($socialMedia) => [ 'code' => $socialMedia->code, 'icon' => $socialMedia->icon, 'name' => $socialMedia->name, 'url' => $socialMedia->pivot->url, ]) ->values() ), 'menues' => $this->whenLoaded( 'menues', fn () => $this->menuTree($this->menues) ), 'categories' => $this->whenLoaded( 'categories', fn () => $this->categoryTree($this->categories) ), ]; } /** * @param Collection $categories * @return Collection> */ private function categoryTree(Collection $categories): Collection { $categoryIds = $categories->pluck('id')->flip(); $childrenByParent = $categories ->filter(fn (Category $category) => $category->categoria_id !== null && $categoryIds->has($category->categoria_id)) ->groupBy('categoria_id'); $formatCategory = function (Category $category) use (&$formatCategory, $childrenByParent): array { return [ 'id' => $category->id, 'nombre' => $category->nombre, 'subcategories' => $childrenByParent ->get($category->id, collect()) ->map($formatCategory) ->values(), ]; }; return $categories ->filter(fn (Category $category) => $category->categoria_id === null || ! $categoryIds->has($category->categoria_id)) ->map($formatCategory) ->values(); } /** * @param Collection $menus * @return Collection> */ private function menuTree(Collection $menus): Collection { $menuCodes = $menus->pluck('code')->flip(); $childrenByParent = $menus ->filter(fn (Menu $menu) => $menu->parent_menu_code !== null) ->groupBy('parent_menu_code'); $formatMenu = function (Menu $menu) use (&$formatMenu, $childrenByParent): array { $formatted = [ 'id' => $menu->id, 'code' => $menu->code, 'label' => $menu->label, 'parent_menu_code' => $menu->parent_menu_code, 'content_type' => $menu->content_type, 'route' => $menu->route, ]; if ($menu->pivot?->static_content !== null) { $formatted['static_content'] = $menu->pivot->static_content; } $formatted['submenues'] = $childrenByParent ->get($menu->code, collect()) ->map($formatMenu) ->values(); return $formatted; }; return $menus ->filter(fn (Menu $menu) => $menu->parent_menu_code === null || ! $menuCodes->has($menu->parent_menu_code)) ->map($formatMenu) ->values(); } }