私は、簡単な学校教育のプラトンフォームを開発しています。ここでは、各学生laravelを使って階層テーブルを作成する方法は?
id
theme_id
description
まあの作品になります、私が作るしようとしている - 各ジョブのテーマは
id
parent_id
name
works
を保存されます - themes
:厚底2つのテーブルを持っています階層テーブル。各行にはテーマがあり、行を開くとテーマの作品が表示され、子テーマがある場合はこれとリンクが表示されます。このような
、私が持っている:私のコントローラで
を(私はこれを改善する必要があります):
public function index(){
$themes = DB::table('themes', DB::raw('SELECT * WHERE lft IS BETWEEN parent.lft AND parent.rgt'))->get();
$works = Work::all();
$themes = array_map(function($theme){
$theme->works = [];
return $theme;
}, $themes->toArray());
foreach($themes as $theme){
foreach($works as $work){
if($theme->id === $work->theme_id){
$theme->works[] = $work;
}
}
}
return view('dashboard.trabalhos.index', ['themes' => $themes]);
}
コントローラのリターン関数:
array:3 [▼
0 => {#275 ▼
+"id": 1
+"parent_id": null
+"name": "Tema1"
+"description": "asdasdasd"
+"lft": 1
+"rgt": 6
+"depth": 0
+"created_at": null
+"updated_at": null
+"works": array:1 [▼
0 => Work {#287 ▶}
]
}
1 => {#278 ▶}
2 => {#279 ▶}
]
とビューで、私の最後のテストでは、このようなものだった:
<table id="asd" class="highlight">
<thead>
<tr>
<th>Tema</th>
</tr>
</thead>
<tbody>
@forelse($themes as $theme)
@php($asd = $theme->depth)
@if($theme->depth === 0)
<tr data-tt-id="{{$theme->id}}" class="branch collapsed">
<td>
@if(count($theme->works) > 0)
<?php
foreach($theme->works as $work){
echo "nome: " . $work->title;
}
?>
@endif
{{$theme->name}}
</td>
</tr>
@elseif($theme->depth > 0)
<tr data-tt-id="{{$theme->id}}" data-tt-parent-id="{{$theme->parent_id}}" class="branch collapsed" style="display: none;">
<td>
{{$theme->name}}
</td>
</tr>
@endif
@empty
@endforelse
<tr></tr>
</tbody>
</table>
私はbootstrap-treeview
とtreetable
を使用してみましたが、多分私の問題は、フロントエンドでの論理です。誰かが私を助けて私に光を与えることができますか?
することができますあなたのコードのいくつかを表示しますか? – Drudge
このチュートリアルを試すhttp://itsolutionstuff.com/post/laravel-5-category-treeview-hierarchical-structure-example-with-demoexample.html – manjunath
再帰的な方法が必要 –