Skip to content
Advertisement

How can I show subcategories in categories?

Here’s the 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 (IlluminateDatabaseQueryException $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

Advertisement

Answer

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 (IlluminateDatabaseQueryException $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'
                    )
    )
)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement