Skip to content
Advertisement

create API for multidimensonal array using category, subcategory and further subcategory laravel

I have to create API for dynamic menu used in react js. The menu contains catgeory, subcategory and further subcategory. I have created the code, but it is not working as expected.

The required output is,

{
  "data": [
    {
      "id": 15,
      "name": "category 1",
      "slug": "category-1",
      "subcategory": [
        {
            "sub_id": 15,
            "sub_name": "subcategory 1",
            "sub_slug": "subcategory-1"
            "fsubcategory": [
              {
                "fsub_id": 15,
                "fsub_name": "fsubcategory 1",
                "fsub_slug": "fsubcategory-1"
              },
              ...
            ]
        },
        ...
      ]
    },
    ....
  ]
}

The code I have used to create the above result is,

$list = Category::select('id', 'name', 'slug')->where(array('in_menu' => 1, 'status' => 1, 'deleted_at' => null))->orderBy('id', 'desc')->get();

    $data = [];

    foreach ($list as $category) {
        $sublist = Subcategory::select('id', 'name', 'slug')->where(array('category_id' => $category->id, 'status' => 1, 'deleted_at' => null))->orderBy('id', 'desc')->get();

        $item = [
            "id" => $category->id,
            "name" => $category->name,
            "slug" => $category->slug,
            "subcategory" => [],
        ];

        foreach($sublist as $subcategory) {
            $fsublist = Fsubcategory::select('id', 'name', 'slug')->where(array('category_id' => $category->id, 'subcategory_id' => $subcategory->id, 'status' => 1, 'deleted_at' => null))->orderBy('id', 'desc')->get();

            $item["subcategory"][] = [
                "sub_id" => $subcategory->id,
                "sub_name" => $subcategory->name,
                "sub_slug" => $subcategory->slug,
                "fsubcategory" => [],
            ];

            foreach($fsublist as $fsubcategory) {
                $item["subcategory"][]["fsubcategory"] = [
                    "fsub_id" => $fsubcategory->id,
                    "fsub_name" => $fsubcategory->name,
                    "fsub_slug" => $fsubcategory->slug,
                ];
            }
        }

        $data[] = $item;
    }

    return response()->json(['data' => $data]);

But the above code is not getting the required output. The output for the above code is,

    {
"data": [
    {
    "id": 11,
    "name": "dhdf dfheryer",
    "slug": "dhdf-dfheryer",
    "subcategory": [
        
    ]
    },
    {
    "id": 10,
    "name": "dfhdfdf hery1----",
    "slug": "fhdfdf-hery1",
    "subcategory": [
        {
        "sub_id": 5,
        "sub_name": "sdgwet gjsdg",
        "sub_slug": "sdgwet-gjsdg",
        "fsubcategory": [
            
        ]
        }
    ]
    },
    {
    "id": 1,
    "name": "dfhdfh",
    "slug": "dfhdf",
    "subcategory": [
        {
        "sub_id": 7,
        "sub_name": "FSubcategory 1",
        "sub_slug": "fsubcategory-1",
        "fsubcategory": [
            
        ]
        },
        {
        "sub_id": 2,
        "sub_name": "Test Subcategory",
        "sub_slug": "test-subcategory",
        "fsubcategory": [
            
        ]
        },
        [
        {
            "fsubcategory": {
            "fsub_id": 2,
            "fsub_name": "dfhdf dfhdfh",
            "fsub_slug": "dfhdf-dfhdfh"
            }
        }
        ],
        {
        "sub_id": 1,
        "sub_name": "ssdgsdg",
        "sub_slug": "sgsdg",
        "fsubcategory": [
            
        ]
        },
        [
        {
            "fsubcategory": {
            "fsub_id": 3,
            "fsub_name": "dfdfh",
            "fsub_slug": "dfdfh"
            }
        }
        ],
        [
        {
            "fsubcategory": {
            "fsub_id": 1,
            "fsub_name": "fdfd",
            "fsub_slug": "fdfd"
            }
        }
        ]
    ]
    }
]
}

fsubcategory is outside subcategory. How to get the required output. How to modify the code to achieve the correct result. I am really stuck here.

Advertisement

Answer

I have got the answer,

$list = Category::select('id', 'name', 'slug')
    ->where([
        'in_menu' => 1,
        'status' => 1,
        'deleted_at' => null
    ])
    ->orderBy('id', 'desc')
    ->get();

$data = [];

foreach ($list as $category) {
    $sublist = Subcategory::select('id', 'name', 'slug')
        ->where([
            'category_id' => $category->id,
            'status' => 1,
            'deleted_at' => null
        ])
        ->orderBy('id', 'desc')
        ->get();

    $item = [
        "id" => $category->id,
        "name" => $category->name,
        "slug" => $category->slug,
        "subcategory" => [],
    ];

    foreach ($sublist as $subcategory) {
        $fsublist = Fsubcategory::select('id', 'name', 'slug')->where(array('subcategory_id' => $subcategory->id, 'status' => 1, 'deleted_at' => null))->orderBy('id', 'desc')->get();

        $subitems = [];
        foreach ($fsublist as $fsubcategory) {
            $subitems[] = [
                "fsub_id" => $fsubcategory->id,
                "fsub_name" => $fsubcategory->name,
                "fsub_slug" => $fsubcategory->slug,
            ];
        }

        $item["subcategory"][] = [
            "sub_id" => $subcategory->id,
            "sub_name" => $subcategory->name,
            "sub_slug" => $subcategory->slug,
            "fsubcategory" => $subitems,
        ];
    }

    $data[] = $item;
}

return response()->json(['response' => 200, 'data' => $data]);

This will provide the required output

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement