【问题标题】:Laravel - hierarchy recursionLaravel - 层次递归
【发布时间】:2021-09-07 15:32:13
【问题描述】:

我有一个marketer 表,每个营销人员可能有多个营销人员,也可能属于一个营销人员。我已经编写了这个方法来检查我是否可以获得一个特定营销人员的多个级别:

表:

id   parent_id    name
1    NULL         a
2    1            b
3    2            c
4    1            d
5    3            e
6    2            f
7    4            g

营销者模型:

public function parent()
{
    return $this->belongsTo(Self::class);
}

public function children()
{
    return $this->hasMany(Self::class,'parent_id');
}

方法:

public function marketerWithSubs(Marketer $marketer, $index = 1, $output = [])
{
    $output = [];
    foreach ($marketer->children as $firstLevel){
        $output[1][] = $firstLevel->id . '- ' . $firstLevel->full_name;
        foreach ($firstLevel->children as $secondLevel){
            $output[2][] = $secondLevel->id . '- ' . $secondLevel->full_name;
            foreach ($secondLevel->children as $thirdLevel) {
                $output[3][] = $thirdLevel->id . '- ' . $thirdLevel->full_name;
                foreach ($thirdLevel->children as $forthLevel) {
                    $output[4][] = $forthLevel->id . '- ' . $forthLevel->full_name;
                    foreach ($forthLevel->children as $fifthLevel) {
                        $output[5][] = $fifthLevel->id . '- ' . $fifthLevel->full_name;
                        foreach ($firstLevel->children as $sixthLevel) {
                            $output[6][] = $sixthLevel->id . '- ' . $sixthLevel->full_name;
                        }
                    }
                }
            }
        }
    }

    return $output;
}

方法示例输出,这是我所期望的,数组中的索引,显示(所谓的)submarketer 的级别,值是 submarketers ids 作为数组:

array:6 [▼
  1 => array:27 [▶]
  2 => array:23 [▶]
  3 => array:32 [▶]
  4 => array:8 [▶]
  5 => array:5 [▼
    0 => 102
    1 => 63
    2 => 64
    3 => 65
    4 => 67
  ]
  6 => array:35 [▶]
]

如何将其转化为实用的方法?

【问题讨论】:

  • 你能不能表现出关系以及期待。所以很容易想到。谢谢

标签: php laravel recursion


【解决方案1】:

好吧,我想我找到了答案:

public function getMarketerWithSubs3(Marketer $marketer, $index = 1, $output = [])
{
    foreach ($marketer->children as $firstLevel){
        $output[$index][] = $firstLevel->id;
        $output = $this->getMarketerWithSubs3($firstLevel, $index+1, $output);
    }

    return $output;
}

【讨论】:

    【解决方案2】:

    $output 可能必须通过引用传递才能使其工作

    public function marketerWithSubs(Marketer $marketer, $index = 1, &$output = [])
    {
        return array_reduce($marketer->children->all(), function($output, $child) {
            $output[$index][] = $child->id . '- ' . $child->full_name;
            if ($child->children->count()) {
                $this->marketerWithSubs($child, $index++, $output);
            }
            return $output;
        }, $output);
    }
    

    【讨论】:

    • 谢谢,但是这种方法不起作用。 array_reduce 中的第一个参数必须是数组,但给出了对象。我将第一个参数更改为数组,但没有运气。
    • 你试过$marketer->children->all()吗?
    • 我会尝试新的解决方案。
    • Trying to get property 'id' of non-object
    • 我太傻了。忘记将 $firstLevel 重命名为 $child 以保持一致性。更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 2017-02-04
    • 1970-01-01
    • 2014-01-25
    • 2021-07-04
    • 1970-01-01
    相关资源
    最近更新 更多