category qosishsh to'g'irlandi
This commit is contained in:
@@ -82,17 +82,7 @@ class Controller extends ExController
|
||||
{
|
||||
if ($request->isMethod('get')) {
|
||||
$this->authorize('create', 'products');
|
||||
$categories = $this->categories->select('id', 'name->ru as category')
|
||||
->where('parent_id', null)
|
||||
->with([
|
||||
'parents' => function ($parent) {
|
||||
return $parent->select('id', 'name->ru as category', 'parent_id')->with([
|
||||
'parents' => function ($parent) {
|
||||
return $parent->select('id', 'name->ru as category', 'parent_id');
|
||||
}
|
||||
]);
|
||||
}
|
||||
])->get();
|
||||
$categories = $this->categoryTree();
|
||||
$brands = $this->brands->get();
|
||||
$colors = $this->colors->get();
|
||||
$measurement = Measurement::query()->get();
|
||||
@@ -146,6 +136,31 @@ class Controller extends ExController
|
||||
}
|
||||
}
|
||||
|
||||
private function categoryTree($parentId = null, array $visited = [])
|
||||
{
|
||||
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', 'asc')
|
||||
->get()
|
||||
->map(function (Category $category) use ($visited) {
|
||||
$children = in_array($category->id, $visited, true)
|
||||
? collect()
|
||||
: $this->categoryTree($category->id, [...$visited, $category->id]);
|
||||
|
||||
return [
|
||||
'id' => $category->id,
|
||||
'category' => $category->name['ru'] ?? $category->name['uz'] ?? '',
|
||||
'parent_id' => $category->parent_id,
|
||||
'parents' => $children,
|
||||
'$isDisabled' => $children->isNotEmpty(),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @return array
|
||||
@@ -309,17 +324,7 @@ class Controller extends ExController
|
||||
}
|
||||
|
||||
|
||||
$categories = $this->categories->select('id', 'name->ru as category')
|
||||
->where('parent_id', null)
|
||||
->with([
|
||||
'parents' => function ($parent) {
|
||||
return $parent->select('id', 'name->ru as category', 'parent_id')->with([
|
||||
'parents' => function ($parent) {
|
||||
return $parent->select('id', 'name->ru as category', 'parent_id');
|
||||
}
|
||||
]);
|
||||
}
|
||||
])->get();
|
||||
$categories = $this->categoryTree();
|
||||
|
||||
$brands = $this->brands->get();
|
||||
$measurement = Measurement::query()->get();
|
||||
|
||||
Reference in New Issue
Block a user