简体   繁体   中英

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
    • Sub 2
  2. Category
    • Sub 1
    • Sub 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 . 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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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