Saya harus membuat API untuk menu dinamis yang digunakan dalam reaksi js. Menu berisi kategori, subkategori dan subkategori lebih lanjut. Saya telah membuat kode, tetapi tidak berfungsi seperti yang diharapkan.

Keluaran yang dibutuhkan adalah,

{
  "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"
              },
              ...
            ]
        },
        ...
      ]
    },
    ....
  ]
}

Kode yang saya gunakan untuk membuat hasil di atas adalah,

$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]);

Tetapi kode di atas tidak mendapatkan output yang diperlukan. Output dari kode di atas adalah,

    {
"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 berada di luar subcategory. Bagaimana mendapatkan output yang dibutuhkan. Cara memodifikasi kode untuk mencapai hasil yang benar. Saya benar-benar terjebak di sini.

0
Jithin Varghese 7 Juli 2020, 20:48

1 menjawab

Jawaban Terbaik

Saya sudah mendapat jawabannya,

$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]);

Ini akan memberikan output yang dibutuhkan

0
Jithin Varghese 8 Juli 2020, 03:44