diff --git a/app/Endpoint/Api/Http/Controllers/Product/ProductCategoryController.php b/app/Endpoint/Api/Http/Controllers/Product/ProductCategoryController.php index b2f6e378..4c110069 100644 --- a/app/Endpoint/Api/Http/Controllers/Product/ProductCategoryController.php +++ b/app/Endpoint/Api/Http/Controllers/Product/ProductCategoryController.php @@ -3,22 +3,29 @@ namespace App\Endpoint\Api\Http\Controllers\Product; use App\Endpoint\Api\Http\Controllers\Controller; -use App\Endpoint\Api\Http\Resources\ProductCategoryResource; use App\Models\ProductCategory; use Illuminate\Http\Request; class ProductCategoryController extends Controller { - /** - * 商品分类 - * - * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\JsonResponse - */ public function index(Request $request) { - $categories = ProductCategory::filter(['pid' => 2])->latest('sort')->get(); + $list = ProductCategory::showable()->latest('sort')->get(); + $data = $this->tree($list); - return ProductCategoryResource::collection($categories); + return response()->json($data); + } + + protected function tree($list, $pid = null) + { + $items = $list->where('parent_id', $pid); + $data = []; + foreach($items as $item) { + $sub_data = ['id' => $item->id, 'name' => $item->name, 'icon' => $item->icon ?: '']; + $children = $this->tree($list, $item->id); + $sub_data['children'] = $children; + array_push($data, $sub_data); + } + return $data; } } diff --git a/app/Endpoint/Api/Http/Resources/ProductCategoryResource.php b/app/Endpoint/Api/Http/Resources/ProductCategoryResource.php deleted file mode 100644 index 52570aa4..00000000 --- a/app/Endpoint/Api/Http/Resources/ProductCategoryResource.php +++ /dev/null @@ -1,23 +0,0 @@ - $this->id, - 'name' => $this->name, - 'icon' => (string) $this->icon, - ]; - } -}