category qosishsh to'g'irlandi

This commit is contained in:
2026-04-29 14:04:56 +05:00
parent a28f552f96
commit efc648bcd7
3 changed files with 57 additions and 20 deletions

View File

@@ -50,7 +50,7 @@ class Controller extends ExController
if ($request->isMethod('get')) { if ($request->isMethod('get')) {
$this->authorize('create', 'compilations'); $this->authorize('create', 'compilations');
$categories = $this->categories->whereNull('parent_id')->orderBy('position')->get(); $categories = $this->categoryOptions();
return view('dashboard.compilations.store', compact('categories')); return view('dashboard.compilations.store', compact('categories'));
} }
@@ -86,7 +86,7 @@ class Controller extends ExController
$compilation->setRelation('products', $products); $compilation->setRelation('products', $products);
$categories = $this->categories->whereNull('parent_id')->orderBy('position')->get(); $categories = $this->categoryOptions();
return view('dashboard.compilations.update', compact('compilation', 'categories')); return view('dashboard.compilations.update', compact('compilation', 'categories'));
@@ -115,6 +115,11 @@ class Controller extends ExController
->when($categoryId > 0, function ($builder) use ($categoryId) { ->when($categoryId > 0, function ($builder) use ($categoryId) {
$categoryIds = $this->categoryIdsWithChildren($categoryId); $categoryIds = $this->categoryIdsWithChildren($categoryId);
if (empty($categoryIds)) {
$builder->whereRaw('1 = 0');
return;
}
$builder->whereHas('categories', function ($category) use ($categoryIds) { $builder->whereHas('categories', function ($category) use ($categoryIds) {
$category->whereIn('categories.id', $categoryIds); $category->whereIn('categories.id', $categoryIds);
}); });
@@ -146,25 +151,57 @@ class Controller extends ExController
private function categoryIdsWithChildren(int $categoryId): array private function categoryIdsWithChildren(int $categoryId): array
{ {
$category = Category::with('children.children')->find($categoryId); if (!Category::whereKey($categoryId)->exists()) {
if (!$category) {
return []; return [];
} }
$ids = [$category->id]; return $this->collectCategoryIds($categoryId);
foreach ($category->children as $child) {
$ids[] = $child->id;
foreach ($child->children as $grandChild) {
$ids[] = $grandChild->id;
}
}
return $ids;
} }
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 * @param Compilation $compilation

View File

@@ -50,7 +50,7 @@
<multiselect v-model="compilations" <multiselect v-model="compilations"
placeholder="Искать" placeholder="Искать"
label="name" label="name"
track-by="name" track-by="id"
:options="products" :options="products"
:option-height="104" :option-height="104"
:custom-label="customLabel" :custom-label="customLabel"
@@ -168,7 +168,7 @@
}), }),
mounted() { mounted() {
this.SearchProduct('');
}, },
methods: { methods: {

View File

@@ -50,7 +50,7 @@
<multiselect v-model="compilations" <multiselect v-model="compilations"
placeholder="Искать" placeholder="Искать"
label="name" label="name"
track-by="name" track-by="id"
:options="products" :options="products"
:option-height="104" :option-height="104"
:custom-label="customLabel" :custom-label="customLabel"
@@ -171,7 +171,7 @@
}, },
mounted() { mounted() {
this.SearchProduct('');
}, },
methods: { methods: {