展平 laravel 嵌套关系(父级到子级)

问题描述 投票:0回答:1

我的模型上有亲子关系。我想要的是压平所有这些集合,包括父母的所有后代。

[
   {
      'name': 'Parent',
      'another_array' => [

          {
             'name': 'Another Array Name'
          }
      ],
      'children': [
         {
             'name': 'First Child',
             'address': 'lorem ipsum',
             'contact_no': '92390',


             'another_array' => [

                 {
                    'name': 'Another Array Name'
                 }
             ],
             'children': [
                {
                    'name': 'First Descendant',
                    'address': 'lorem ipsum',
                    'contact_no': '92390',
                    'children': [
                        {
                            'name': 'Descendant Child',
                            'address': 'lorem ipsum',
                            'contact_no': '92390',
                            'children': [
                                {
                                   'name': 'Last Descendant',
                                   'address': '',
                                   'contact_no': '',
                                   'children': []
                                }
                            ]

                        }
                    ]
                }
             ]
         }
      ]
   }
]


我尝试了下面的代码。然而,它仅显示儿童的第二深度。它不显示集合的下一个子项。

User.php
public function parent()
{
    return $this->belongsTo(User::class, 'id', 'parent_id');
}

public function children()
{

    return $this->hasMany(User::class,'parent_id', 'id');
}

public function descendants()
{
    return $this->children()->with('descendants');
}

UsersController.php

public function index()
{
    $parent = User::find(1);

    $parent->with('descendants')->find($parent->id);

    $descendants = $this->traverseTree($parent->descendants);

    return response($descendants);
}

protected function traverseTree($subtree)
{
    $descendants = collect([]);

    foreach ($subtree->descendants as $descendant) {

       $descendants->push($descendant);

       if($descendant->descendants instanceof Collection) {

          foreach ($descendant->descendants as $node) {

             $this->traverseTree($node);

             $descendants->push($node);
          }
       }
   }

   return $descendants;
}

所需输出


    [
        {
            'name': 'Parent',
            'address': 'lorem ipsum',
            'contact_no': '92390'
        },
        {
            'name': 'First Child',
            'address': 'lorem ipsum',
                 'contact_no': '92390'
        },
        {
            'name': 'First Descendant',
            'address': 'lorem ipsum',
                 'contact_no': '92390',
        },
        {
            'name': 'Descendant Child',
            'address': 'lorem ipsum',
                 'contact_no': '92390',
        },
        {
            'name': 'Last Descendant' ,
            'address': '',
                 'contact_no': '',
        }

    ]

我期待您的帮助。

php laravel laravel-5 eloquent
1个回答
2
投票

通过链接

toArray()
方法将您的集合响应转换为数组,以便输出成为多维数组而不是嵌套对象,如下所示...

       $my_array = [
            'name' => 'Parent',
            'children' => [
                 'name' => 'First Child',
                 'address' => 'lorem ipsum',
                 'contact_no' => '234',
                 'children' => [
                    'name' => 'First Descendant',
                     'address' => 'lorem ipsum',
                     'contact_no' => '567',
                    'children' => [
                        'name' => 'Descendant Child',
                        'address' => 'lorem ipsum',
                        'contact_no' => '8890',
                        'children' => [
                            'name' => 'Last Descendant',
                            'address' => '',
                            'contact_no' => '',
                            'children' =>  []
                        ]
                    ]
                 ]
            ]
        ];

然后你可以使用下面这个辅助函数:

protected function _flattened($array)
    {
        $flatArray = [];

        if (!is_array($array)) {
            $array = (array)$array;
        }

        foreach($array as $key => $value) {
            if (is_array($value) || is_object($value)) {
                $flatArray = array_merge($flatArray, $this->_flattened($value));
            } else {
                $flatArray[0][$key] = $value;
            }
        }

        return $flatArray;
    }

当你转储

$result = $this->_flattened($my_array);
时,你会得到类似的输出...

       [
            0 => [
                "name" => "Parent"
            ],
            1 => [
                "name" => "First Child",
                "address" => "lorem ipsum"
                "contact_no" => "234"
            ],
            2 => [
                "name" => "First Descendant"
                "address" => "lorem ipsum"
                "contact_no" => "567"
            ],
            3 => [
                "name" => "Descendant Child"
                "address" => "lorem ipsum"
                "contact_no" => "8890"
            ],
            4 => [
                "name" => "Last Descendant"
                "address" => ""
                "contact_no" => ""
            ],
        ];
© www.soinside.com 2019 - 2024. All rights reserved.