Php 在laravel刀片中显示树结构

Php 在laravel刀片中显示树结构,php,laravel,Php,Laravel,我试图在blade中显示树结构,我需要一些帮助 这是我的算法 public function fetchData($entry_id) { $results = TreeEntry::where('parent_entry_id', $entry_id)->get(); $treeEntryList = []; foreach ($results as $result) { $data =

我试图在blade中显示树结构,我需要一些帮助

这是我的算法

public function fetchData($entry_id)
    {
        $results = TreeEntry::where('parent_entry_id', $entry_id)->get();

        $treeEntryList = [];
        
        foreach ($results as $result) {

            $data = [
                'id' => $result->entry_id,
                'parent_entry_id' => $result->parent_entry_id,
                'children' => $this->fetchData($result->entry_id)
            ];

            $treeEntryList[] = $data;
        }
然后得到这种树数组

尝试类似的方法,但只显示第一个子节点,我想显示所有元素。 你知道怎么解决这个问题吗?这里是递归还是其他什么

@extends('layouts.master')
@section('content')
    {{dd($resultList)}}
<div class="container">
    <br id="lang-container">
        @foreach ($resultList as $result)
            <div>{{$result['id']}}</div></br>
            @foreach ($result['children'] as $child)
                <div>{{$child['id']}}</div></br>
            @endforeach
       
        @endforeach
    </div>

@endsection
@section('js')
@endsection
@extends('layouts.master'))
@节(“内容”)
{{dd($resultList)}

@foreach($resultList作为$result) {{$result['id']}
@foreach($result['children']作为$child) {{$child['id']}
@endforeach @endforeach @端部 @节('js') @端部
在我看来,我的想法是使用组件,但还没有真正使用它们,所以,不确定这是否有效。首先,运行php artisan make:component TreeentryList来创建组件。然后,转到创建的组件,插入这段代码(如果有任何问题,请修复它)

@foreach($children作为$child)
{{$child['id']}
@如果(isset($children['children'])) //如果它有语法错误,请使用“for children” @恩迪夫 @endforeach
然后,转到TreeentryList文件并将$children添加到其构造函数:


<?php

namespace App\View\Components;

use Illuminate\View\Component;

class TreeEntryListing extends Component
{
    public $children;

    /**
     * Create a new component instance.
     *
     * @param $children
     */
    public function __construct($children)
    {
        $this->children = $children;
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|string
     */
    public function render()
    {
        return view('components.tree-entry-listing');
    }
}


我的想法是使用组件,但还没有真正使用它们,因此,不确定这是否有效。首先,运行
php artisan make:component TreeentryList
,创建组件。然后,转到创建的组件,插入这段代码(如果有任何问题,请修复它)

@foreach($children作为$child)
{{$child['id']}
@如果(isset($children['children'])) //如果它有语法错误,请使用“for children” @恩迪夫 @endforeach
然后,转到TreeentryList文件并将$children添加到其构造函数:


<?php

namespace App\View\Components;

use Illuminate\View\Component;

class TreeEntryListing extends Component
{
    public $children;

    /**
     * Create a new component instance.
     *
     * @param $children
     */
    public function __construct($children)
    {
        $this->children = $children;
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|string
     */
    public function render()
    {
        return view('components.tree-entry-listing');
    }
}

...
@foreach ($resultList as $result)
            <div>{{$result['id']}}</div></br>
            <x-tree-entry-listing children={{$result['children']}} />
       
@endforeach
...