1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-24 12:05:39 +08:00
course-tencent-cloud/app/Builders/CategoryTreeList.php
2024-04-10 19:00:53 +08:00

66 lines
1.5 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Builders;
use App\Models\Category as CategoryModel;
use App\Repos\Category as CategoryRepo;
class CategoryTreeList extends Builder
{
public function handle($type)
{
$categoryRepo = new CategoryRepo();
$topCategories = $categoryRepo->findTopCategories($type);
if ($topCategories->count() == 0) {
return [];
}
$list = [];
foreach ($topCategories as $category) {
$list[] = [
'id' => $category->id,
'name' => $category->name,
'alias' => $category->alias,
'icon' => $category->icon,
'children' => $this->handleChildren($category),
];
}
return $list;
}
protected function handleChildren(CategoryModel $category)
{
$categoryRepo = new CategoryRepo();
$subCategories = $categoryRepo->findChildCategories($category->id);
if ($subCategories->count() == 0) {
return [];
}
$list = [];
foreach ($subCategories as $category) {
$list[] = [
'id' => $category->id,
'name' => $category->name,
'alias' => $category->alias,
'icon' => $category->icon,
];
}
return $list;
}
}