From efc648bcd79e3c70f27815d949215327b0fd56be Mon Sep 17 00:00:00 2001 From: husanjon Date: Wed, 29 Apr 2026 14:04:56 +0500 Subject: [PATCH] category qosishsh to'g'irlandi --- .../Dashboard/Compilation/Controller.php | 69 ++++++++++++++----- resources/js/components/Compilation/Store.vue | 4 +- .../js/components/Compilation/Update.vue | 4 +- 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/Dashboard/Compilation/Controller.php b/app/Http/Controllers/Dashboard/Compilation/Controller.php index 6ef69ef..22973aa 100755 --- a/app/Http/Controllers/Dashboard/Compilation/Controller.php +++ b/app/Http/Controllers/Dashboard/Compilation/Controller.php @@ -50,7 +50,7 @@ class Controller extends ExController if ($request->isMethod('get')) { $this->authorize('create', 'compilations'); - $categories = $this->categories->whereNull('parent_id')->orderBy('position')->get(); + $categories = $this->categoryOptions(); return view('dashboard.compilations.store', compact('categories')); } @@ -86,7 +86,7 @@ class Controller extends ExController $compilation->setRelation('products', $products); - $categories = $this->categories->whereNull('parent_id')->orderBy('position')->get(); + $categories = $this->categoryOptions(); return view('dashboard.compilations.update', compact('compilation', 'categories')); @@ -115,6 +115,11 @@ class Controller extends ExController ->when($categoryId > 0, function ($builder) use ($categoryId) { $categoryIds = $this->categoryIdsWithChildren($categoryId); + if (empty($categoryIds)) { + $builder->whereRaw('1 = 0'); + return; + } + $builder->whereHas('categories', function ($category) use ($categoryIds) { $category->whereIn('categories.id', $categoryIds); }); @@ -146,25 +151,57 @@ class Controller extends ExController private function categoryIdsWithChildren(int $categoryId): array { - $category = Category::with('children.children')->find($categoryId); - - if (!$category) { + if (!Category::whereKey($categoryId)->exists()) { return []; } - $ids = [$category->id]; - - foreach ($category->children as $child) { - $ids[] = $child->id; - - foreach ($child->children as $grandChild) { - $ids[] = $grandChild->id; - } - } - - return $ids; + return $this->collectCategoryIds($categoryId); } + private function collectCategoryIds(int $categoryId, array $visited = []): array + { + if (in_array($categoryId, $visited, true)) { + return []; + } + + $visited[] = $categoryId; + $ids = [$categoryId]; + + $children = Category::where('parent_id', $categoryId) + ->orderBy('position') + ->pluck('id'); + + foreach ($children as $childId) { + $ids = array_merge($ids, $this->collectCategoryIds((int) $childId, $visited)); + } + + return array_values(array_unique($ids)); + } + + private function categoryOptions($parentId = null, string $prefix = '') + { + return Category::select('id', 'name', 'parent_id') + ->when($parentId === null, function ($query) { + $query->whereNull('parent_id'); + }, function ($query) use ($parentId) { + $query->where('parent_id', $parentId); + }) + ->orderBy('position') + ->get() + ->flatMap(function (Category $category) use ($prefix) { + $ru = $category->name['ru'] ?? $category->name['uz'] ?? ''; + $uz = $category->name['uz'] ?? $ru; + + return collect([[ + 'id' => $category->id, + 'name' => [ + 'ru' => $prefix . $ru, + 'uz' => $prefix . $uz, + ], + ]])->merge($this->categoryOptions($category->id, $prefix . '- ')); + }) + ->values(); + } /** * @param Compilation $compilation diff --git a/resources/js/components/Compilation/Store.vue b/resources/js/components/Compilation/Store.vue index 1aa2368..59d6ed7 100755 --- a/resources/js/components/Compilation/Store.vue +++ b/resources/js/components/Compilation/Store.vue @@ -50,7 +50,7 @@