laravel - 在树视图中显示数据在树视图中显示数据
问题描述
我正在研究 Laravel。我正在尝试在树视图中显示我的数据。但我成功只是第一步。你们有人可以帮忙吗?
代码:
<button class=" btn-primary" type="button" data-toggle="collapse" data-target=".multi-collapse" aria-expanded="false" aria-controls=".multi-collapse" >
<span class="glyphicon glyphicon-eye-open"></span></button>
@foreach(App\Account::where('nomencloture', 'LIKE', '_')->get() as $item)
<div class="collapse multi-collapse" id="{{$item->nomencloture}}" >
<div class="list-group-item " >
<div class="col-md-2"> {{ $item->account_id }}</div>
<div class="col-md-2"> {{ $item->account_name }}</div>
<button class=" btn-primary" type="button" data-toggle="collapse" data-target="#PUT SOMETHINGHERE" aria-expanded="false" aria-controls="multiCollapseExample1" >
<span class="glyphicon glyphicon-eye-open"></span></button>
<br>
<br>
@foreach(App\Account::where('nomencloture', 'LIKE', '_______')->get() as $subitem)
<div class="collapse" id="{{ $subitem->nomencloture }}" >
<br>
<div class="list-group-item ">
<div class="col-md-2"> {{ $subitem->account_id }}</div>
<div class="col-md-2"> {{ $subitem->account_name }}</div>
</div>
</div>
@endforeach
</div>
</div>
@endforeach
解决方案
您需要对代码进行一些更改,请在下面找到更改代码的步骤
第 1 步:请在您的模型文件中创建与您的外键的关系,例如
namespace App;
use Illuminate\Database\Eloquent\Model;
class Treeview extends Model
{
protected $table = 'treeview';
public $fillable = ['title','parent_id'];
/**
* Get the index name for the model.
*
* @return string
*/
public function childs() {
return $this->hasMany('App\Treeview','parent_id','id') ;
}
}
第 2 步:在您的控制器中添加以下功能,并根据您的表格和模型对象进行一些更改
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function manageCategory()
{
$categories = Treeview::where('parent_id', '=', 0)->get();
return view('treeview/categoryTreeview',compact('categories')); // set the path of you templates file.
}
第 3 步:在您的 categoryTreeview 模板文件中添加以下代码
<h3>Category List</h3>
<ul id="tree1">
@foreach($categories as $category)
<li>
{{ $category->title }}
@if(count($category->childs))
@include('treeview/manageChild',['childs' => $category->childs]) // Please create another templates file
@endif
</li>
@endforeach
</ul>
第 4 步:请创建另一个模板文件 manageChild 添加以下代码。
<ul>
@foreach($childs as $child)
<li>
{{ $child->title }}
@if(count($child->childs))
@include('treeview/manageChild',['childs' => $child->childs])
@endif
</li>
@endforeach
</ul>
输出显示如下:
Category List
Cat_1
Cat_1_par_1
Cat_1_par_2
Cat_1_par_3
Cat_2
Cat_2_par_1
Cat_2_par_2
Cat_2_par_3
Cat_3
更多信息我找到了一个链接:https ://itsolutionstuff.com/post/laravel-5-category-treeview-hierarchical-structure-example-with-demoexample.html
推荐阅读
- java - 抽象的目的是什么?
- c - 将 x 进行算术左移 n 位 (C)
- azure - 如何将 azure terraform 变量应用于多个 tf 文件一起处理?
- reactjs - contentful / gatsby 中的无限滚动图片库
- r - 文件复制,根据需要制作目录
- vuejs2 - 如何仅在 Vue 2.0 中向 DIRECT PARENT 发送数据?
- ajax - Angular 8获取带有身份验证头的txt文件
- laravel - 如何在laravel中交换数据
- android - 如何在 NDK-BUILD 中构建 arcore camera_utility 共享库?
- reactjs - react ie fetch Unhandled Promise Reject TypeError: Network request failed