【发布时间】:2021-08-06 17:35:11
【问题描述】:
我有像父子这样的数据,在数据库中具有无限级别的子子和子子数据,您可以在下面看到它就像一棵二叉树,其中的节点有多个子节点,这些子节点可以有无限的子子节点。
请给我一个解决方案或建议如何证明这一点。谢谢。
这是我从数据库中获取的数据库 JSON 数据...
控制器代码
$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": []
}
]
}
]
}
]
}
]
},
编辑:我想把上面的数据和这个数据相等
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
]
],
],
];
}
【问题讨论】: