Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉拉维尔:与无尽的孩子们一起构建树结构_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 拉拉维尔:与无尽的孩子们一起构建树结构

Php 拉拉维尔:与无尽的孩子们一起构建树结构,php,laravel,laravel-5,Php,Laravel,Laravel 5,嘿,我正试图和拉威尔一起建立一个有无数孩子的树型结构 这是我用来构建树的: $result = ''; foreach($categories as $root) { $result .= sprintf(' <tr> <td> <div class="radio"> <label>

嘿,我正试图和拉威尔一起建立一个有无数孩子的树型结构

这是我用来构建树的:

$result = '';
    foreach($categories as $root) {

        $result .= sprintf('
            <tr>
                <td>
                    <div class="radio">
                        <label>
                            <input type="radio" name="parent_id" id="" value="%d">
                            %s
                        </label>
                    </div>
                </td>
            </tr>', $root->id, $root->title
        );

        if(isset($root->children)) {
            $this->getChildren($root->children, $result);
        }

    }

    dd($result);
$result='';
foreach($root形式的类别){
$result.=sprintf('
%
“,$root->id,$root->title
);
如果(isset($root->children)){
$this->getChildren($root->children,$result);
}
}
dd(结果);
正如你所看到的,我指的是我的方法中的另一个方法。 那个看起来像这样:

public function getChildren($children, $result)
    {
        foreach($children as $child) {
            $result .= sprintf('
                <tr>
                    <td>
                        <div class="radio">
                            <label>
                                <input type="radio" name="parent_id" id="" value="%d">
                                %s
                            </label>
                        </div>
                    </td>
                </tr>', $child->id, $child->title
            );

            if(isset($child->children)) {
                $this->getChildren($child->children, $result);
            }

        }

        return $result;


    }
公共函数getChildren($children,$result)
{
foreach($childrenas$child){
$result.=sprintf('
%
“,$child->id,$child->title
);
如果(isset($child->children)){
$this->getChildren($child->children,$result);
}
}
返回$result;
}
然而,这只是返回我的主要结果

如何将数据一直附加到$result变量


提前谢谢

将函数声明更改为:

public function getChildren($children, &$result)

&
符号表示通过引用传递变量。

无需重复代码,只需:

$result = $this->getChildren($categories);

dd($result);

公共函数getChildren($children)
{
$result='';
foreach($childrenas$child){
$result.=sprintf('
%
“,$child->id,$child->title
);
如果(isset($child->children)){
$result.=$this->getChildren($child->children);
}
}
返回$result;
}
public function getChildren($children)
{
    $result = '';
    foreach($children as $child) {
        $result .= sprintf('
            <tr>
                <td>
                    <div class="radio">
                        <label>
                            <input type="radio" name="parent_id" id="" value="%d">
                            %s
                        </label>
                    </div>
                </td>
            </tr>', $child->id, $child->title
        );

        if(isset($child->children)) {
            $result .= $this->getChildren($child->children);
        }

    }

    return $result;

}