category qosishsh to'g'irlandi
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -50,7 +50,7 @@
|
||||
<multiselect v-model="compilations"
|
||||
placeholder="Искать"
|
||||
label="name"
|
||||
track-by="name"
|
||||
track-by="id"
|
||||
:options="products"
|
||||
:option-height="104"
|
||||
:custom-label="customLabel"
|
||||
@@ -168,7 +168,7 @@
|
||||
}),
|
||||
|
||||
mounted() {
|
||||
|
||||
this.SearchProduct('');
|
||||
},
|
||||
|
||||
methods: {
|
||||
|
||||
@@ -50,7 +50,7 @@
|
||||
<multiselect v-model="compilations"
|
||||
placeholder="Искать"
|
||||
label="name"
|
||||
track-by="name"
|
||||
track-by="id"
|
||||
:options="products"
|
||||
:option-height="104"
|
||||
:custom-label="customLabel"
|
||||
@@ -171,7 +171,7 @@
|
||||
},
|
||||
|
||||
mounted() {
|
||||
|
||||
this.SearchProduct('');
|
||||
},
|
||||
|
||||
methods: {
|
||||
|
||||
Reference in New Issue
Block a user