简体   繁体   English

如何在类别中显示子类别?

[英]how to show subcategories in categories?

And Here's code这是代码

 public function getSubCategory(Request $request)

{

    $response = array();
    try {
        $categories = Category::where(['status' => 0, 'parent_id' => 0])- >get();
        $subCategory = Category::where('status', 0)->where('parent_id', '!=', 0)->get();

        foreach ($subCategory as $sub) {

            $categoryTitle = Category::where(['id' => $sub->parent_id])- >get(['title']);


            $result[] = array(

                'cat_title' => $categoryTitle[0]->title,
                'sub_title' => $sub->title,

            );
        }
            if (count($result) > 0) {
                $response = (new ApiMessageController())->successResponse($result, "Categories List Found!");
            } else {
                $response = (new ApiMessageController())->failedresponse("No Categories List Found");
            }
    } catch (\Illuminate\Database\QueryException $ex) {
        $response = (new ApiMessageController())->queryexception($ex);
    }
    return $response;
}

I will like to display parent category and its children under it.我想在其下显示父类别及其子项。 Something like this:像这样的东西:

  1. Category类别
    • Sub 1子 1
    • Sub 2子 2
  2. Category类别
    • Sub 1子 1
    • Sub 2子 2

i want to show data like see demo我想像看演示一样显示数据

Database Structure.数据库结构。 Both categories and subcategories are in same table.类别和子类别都在同一个表中。 Database数据库

I suggest u to use NestedSet .我建议你使用NestedSet So you can create a category tree and get all data by depth.因此,您可以创建一个类别树并按深度获取所有数据。

But before you use it, you need to know但是在使用之前,你需要知道

  • Very comfortable to work with tree structure.使用树结构非常舒服。 (+) (+)
  • No recursive select(+)无递归选择(+)
  • You may have a separate, normalized table (+)您可能有一个单独的标准化表 (+)
  • There may be a long request (-)可能有很长的请求 (-)

You can see all the deatils here你可以在这里看到所有的细节

Try this ::尝试这个 ::

public function getSubCategory(Request $request)
{
    $response = array();
    try {
        $allData = array();
        // get all parent category 
        $categories = Category::where(['status' => 0, 'parent_id' => 0])->get();        
        foreach ($categories as $key=>$sub) {
            // now take one by one it's child category 
            $subCategory = Category::where('status', 0)->where('parent_id', '=', $sub->id)->get();
            $subCat = array();
            // set parent category title
            $allData[$key]['parent'] = $sub->title;
            foreach ($subCategory as $k=>$subcat) {
                $subCat[$subcat->id] = $subcat->title
            }
            // set child category array
            $allData[$key]['child'] = $subCat;
        }

        if (count($allData) > 0) {
            $response = (new ApiMessageController())->successResponse($allData, "Categories List Found!");
        } else {
            $response = (new ApiMessageController())->failedresponse("No Categories List Found");
        }
    } catch (\Illuminate\Database\QueryException $ex) {
        $response = (new ApiMessageController())->queryexception($ex);
    }
    return $response;
}

Output:输出:

array(
    0 => array(
        'parent' => 'parent 1',
        'child'  => array(
                        '1' => 'child 1',
                        '2' => 'child 2'
                    )
    ),
    1 => array(
        'parent' => 'parent 2',
        'child'  => array(
                        '1' => 'child 1',
                        '2' => 'child 2'
                    )
    )
)

May be this will help you.可能这会帮助你。

This is how i will proceed:这是我将继续的方式:

  • First i will get all the categories首先,我将获得所有类别
  • Then i will get all the subcategories attached to each of the categories然后我将获得附加到每个类别的所有子类别
  • I will add this in a table我将把它添加到表格中
  • Finally i will return the table.最后我将返回表。

This is how my code looks:这是我的代码的样子:

public function getSubCategory(Request $request){

    $response = array();
    try {
        $categories = Category::where(['status' => 0, 'parent_id' => 0])- >get();
        foreach ($categories as $c) {
            $subcategories = Category::where("parent_id", $c->id)->get();
            $response[] = [$c, $subcategories];
        }

        if (count($response) > 0) {
            $response = (new ApiMessageController())->successResponse($result, "Categories List Found!");
        } else {
            $response = (new ApiMessageController())->failedresponse("No Categories List Found");
        }
    } catch (\Illuminate\Database\QueryException $ex) {
        $response = (new ApiMessageController())->queryexception($ex);
    }
    return $response;
}

Hope my anwser helps !希望我的回答有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM