Skip to content
Advertisement

How can show unlimited parent-child & sub-child tree data in laravel

I have data like parent-child with unlimited levels of sub-child & sub-child data in the database which you can see below like a binary tree with nodes that have multiple children and these children can have unlimited sub-child.

Please give me a solution or suggestion how can show that. Thanks.

This is my database JSON data which I’m getting from the database…

Controller Code

$products = Product::where('project_id', $project_id )->whereNull('parent_id')->with('categories')->with('childProducts')->get();
"data": {
    "products": [
        {
            "id": 1,
            "name": "ParentProduct",
            "project_id": 1,
            "parent_id": null,
            "created_by": 1,
            "created_at": "2021-08-05 10:18:35",
            "updated_at": "2021-08-05 10:18:35",
            "categories": [
                {
                    "id": 6,
                    "product_id": 1,
                    "project_id": 1,
                    "name": "TopLevelCategory",
                    "created_at": "2021-08-05 10:18:35",
                    "updated_at": "2021-08-05 10:18:35",
                }
            ],
            "child_products": [
                {
                    "id": 2,
                    "name": "LevelOneProduct",
                    "project_id": null,
                    "parent_id": 1,
                    "created_by": null,
                    "created_at": "2021-08-05 10:18:35",
                    "updated_at": "2021-08-05 10:18:35",
                    "categories": [
                        {
                            "id": 1,
                            "product_id": 2,
                            "project_id": 1,
                            "name": "LevelOneProductCategory",
                            "created_at": "2021-08-05 10:18:35",
                            "updated_at": "2021-08-05 10:18:35",
                        }
                    ],
                    "child_products": [
                        {
                            "id": 3,
                            "name": "LevelOneProductChildFolder",
                            "project_id": null,
                            "parent_id": 2,
                            "created_by": null,
                            "created_at": "2021-08-05 10:18:35",
                            "updated_at": "2021-08-05 10:18:35",
                            "categories": [
                                {
                                    "id": 2,
                                    "product_id": 3,
                                    "project_id": 1,
                                    "name": "evelOneProductChildFolderCategory",
                                    "created_at": "2021-08-05 10:18:35",
                                    "updated_at": "2021-08-05 10:18:35",
                                }
                            ],
                            "child_products": [
                                {
                                    "id": 4,
                                    "name": "LevelOneProductSubChildFolder",
                                    "project_id": null,
                                    "parent_id": 3,
                                    "created_by": null,
                                    "created_at": "2021-08-05 10:18:35",
                                    "updated_at": "2021-08-05 10:18:35",
                                    "categories": [
                                        {
                                            "id": 3,
                                            "product_id": 4,
                                            "project_id": 1,
                                            "name": "LevelOneProductSubChildFolderCategory",
                                            "created_at": "2021-08-05 10:18:35",
                                            "updated_at": "2021-08-05 10:18:35",                                            
                                        }
                                    ],
                                    "child_products": []
                                }
                            ]
                        }
                    ]
                },
                {
                    "id": 5,
                    "name": "LeveTwoProduct",
                    "project_id": null,
                    "parent_id": 1,
                    "created_by": null,
                    "created_at": "2021-08-05 10:18:35",
                    "updated_at": "2021-08-05 10:18:35",
                    "categories": [],
                    "child_products": [
                        {
                            "id": 6,
                            "name": "LeveTwoProductFolder",
                            "project_id": null,
                            "parent_id": 5,
                            "created_by": null,
                            "created_at": "2021-08-05 10:18:35",
                            "updated_at": "2021-08-05 10:18:35",
                            "categories": [
                                {
                                    "id": 4,
                                    "product_id": 6,
                                    "project_id": 1,
                                    "name": "LeveTwoProductFolderCategory",
                                    "created_at": "2021-08-05 10:18:35",
                                    "updated_at": "2021-08-05 10:18:35",
                                }
                            ],
                            "child_products": [
                                {
                                    "id": 7,
                                    "name": "LeveTwoProductChildFolder",
                                    "project_id": null,
                                    "parent_id": 6,
                                    "created_by": null,
                                    "created_at": "2021-08-05 10:18:35",
                                    "updated_at": "2021-08-05 10:18:35",
                                    "categories": [
                                        {
                                            "id": 5,
                                            "product_id": 7,
                                            "project_id": 1,
                                            "name": "LeveTwoProductChildFolderCategory",
                                            "created_at": "2021-08-05 10:18:35",
                                            "updated_at": "2021-08-05 10:18:35",
                                        }
                                    ],
                                    "child_products": []
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
},

Edit: I want to equal the above data to this data

foreach ($product->categories as $singleCaegory) {                       
    $singleCaegoryData = [
        "name" => $singleCaegory->name, 
        "request" => [ 
            'method' =>  $singleCaegory->type,
            'body' => [
                "mode" => "raw",
                'raw' => $singleCaegory->raw_data,
                "options" => [
                    "raw" => [
                        "language" => "json"
                    ],
                ],
            ],
            'url' =>  [
                "raw" =>  $singleCaegory->url,
                "host" => [
                    $singleCaegory->url
                ]
            ], 
        ],
    ]; 
}

Advertisement

Answer

you need to fetch all the products in one query and then process them to build the tree. You cant eager load it the traditional way. Original solution from Formatink

public function products($projectId)
{
    $products= Product::where('project_id', $projectId)->get();
    $products_by_id = collect();

    foreach ($products as $product) {
        $products_by_id->put($product->id, $product);
    }

    foreach ($products as $key => $product) {
        $products_by_id->get($product->id)->children = new Collection;
        if ($product->parent_id != 0) {
            $products_by_id->get($product->parent_id)->children->push($product);
            unset($products[$key]);
        }
    }

    return $products;
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement