从递归查询的结果获取某些属性数据

发布于 2025-01-30 11:49:52 字数 1890 浏览 1 评论 0原文

以下示例用于填充树并使用带有父列的表。

数据是通过递归查询获得的。

$data = [{
    "id": 1,
    "name": "parent 1"
    "note": "note 1",
}, {
    "id": 2,
    "name": " parent 2",
    "note": "note 2",
    "children": [{
        "id": 21,
        "name": "child A of 2",
        "note": "note A of 2",
    },{
        "id": 22,
        "name": "child B of 2",
        "note": "note B of 2",
    },{
        "id": 23,
        "name": "child C of 2",
        "note": "note C of 2",
        
        "children": [{
            "id": 231,
            "name": "child A of 23",
            "note": "note A of 23",

            "children": [{
                "id": 2311,
                "name": "child A of 231",
                "note": "note A of 231",

                "children": []
            }]
        }]
    }]
}];

和查询:

$myData= Hierarchy::whereNull('parent_id')
                  ->with('children')
                  ->get();

到目前为止还不错。

解决问题:

有必要获得父母和子女的ID和名称属性的简单(非等级)列表。

示例:

"id": 1,
"name": "parent 1",
"id": 2,
"name": " parent 2",
"id": 21,
"name": "child A of 2",
"id": 23,
"name": "child C of 2",
"id": 231,
"name": "child A of 23",
"id": 2311,
"name": "child A of 231"

虽然可以使用JavaScript在客户端解决此问题,但我打算使用雄辩或PHP功能来完成。

我尝试使用array_walk()和array_walk_recursive()php函数(无成功)。

考虑到儿童节点的数量可以是无限的,有什么方法可以解决问题?

谢谢。

编辑:

示例尝试使用array_walk_recursive()php函数

public function getList() 
{
    $myData= Hierarchy::whereNull('parent_id')
                  ->with('children')
                  ->get();
    
    $data = array_walk_recursive($myData, "self::myFunction");

    return response()->json(['success' => true, "data" => $data]);
}

public function myFunction($item, $key){
    ???
}

The following example is used to populate a tree and use a table with a parent_id column.

The data is obtained with a recursive query.

$data = [{
    "id": 1,
    "name": "parent 1"
    "note": "note 1",
}, {
    "id": 2,
    "name": " parent 2",
    "note": "note 2",
    "children": [{
        "id": 21,
        "name": "child A of 2",
        "note": "note A of 2",
    },{
        "id": 22,
        "name": "child B of 2",
        "note": "note B of 2",
    },{
        "id": 23,
        "name": "child C of 2",
        "note": "note C of 2",
        
        "children": [{
            "id": 231,
            "name": "child A of 23",
            "note": "note A of 23",

            "children": [{
                "id": 2311,
                "name": "child A of 231",
                "note": "note A of 231",

                "children": []
            }]
        }]
    }]
}];

And the query:

$myData= Hierarchy::whereNull('parent_id')
                  ->with('children')
                  ->get();

So far so good.

Problem to solve:

It is necessary to obtain a simple (non-hierarchical) list of the id and name attributes of the parents and children.

Example:

"id": 1,
"name": "parent 1",
"id": 2,
"name": " parent 2",
"id": 21,
"name": "child A of 2",
"id": 23,
"name": "child C of 2",
"id": 231,
"name": "child A of 23",
"id": 2311,
"name": "child A of 231"

While this can be solved on the client side with javascript, I intended to do it with eloquent or PHP functions.

I made some attempts with the array_walk() and array_walk_recursive() PHP functions (without success).

Is there any way to solve with eloquent, bearing in mind that the number of children nodes can be infinite?

Thanks.

EDITED:

Example attempt with array_walk_recursive() PHP function

public function getList() 
{
    $myData= Hierarchy::whereNull('parent_id')
                  ->with('children')
                  ->get();
    
    $data = array_walk_recursive($myData, "self::myFunction");

    return response()->json(['success' => true, "data" => $data]);
}

public function myFunction($item, $key){
    ???
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2025-02-06 11:49:52

您可以使用 api resouce 递归或使用递归函数生成hierSarchy大批。

具有递归函数的示例:

function makeHierarchy($values) 
{
    $result = [];

    foreach($values as $item) {
        $result[] = [
            'id' => $item->id,
            'name' => $item->name,
            'children' => makeHierarchy($item->children),
        ];
    }

    return $result;
}

$values = Hierarchy::whereNull('parent_id')->with('children')->get();
$hierarchical = makeHierarchy($values);

如果要将所有值作为平面列表获取:

$values = Hierarchy::get();
$result = [];

foreach($values as $item) {
    $result[] = [
        'id' => $item->id,
        'name' => $item->name,
    ];
}

# now the result contains all the parents and children in a flat list

以清洁的方式:

$result = Hierarchy::select(['id', 'name'])->all();

You can use the API Resouce recursively or use the recursive function to generate the hierarchy array.

Example with recursive function:

function makeHierarchy($values) 
{
    $result = [];

    foreach($values as $item) {
        $result[] = [
            'id' => $item->id,
            'name' => $item->name,
            'children' => makeHierarchy($item->children),
        ];
    }

    return $result;
}

$values = Hierarchy::whereNull('parent_id')->with('children')->get();
$hierarchical = makeHierarchy($values);

If you want to get all values as a flat list:

$values = Hierarchy::get();
$result = [];

foreach($values as $item) {
    $result[] = [
        'id' => $item->id,
        'name' => $item->name,
    ];
}

# now the result contains all the parents and children in a flat list

In the cleaner way:

$result = Hierarchy::select(['id', 'name'])->all();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文