2017-06-26 11 views
2

私は、簡単な学校教育のプラトンフォームを開発しています。ここでは、各学生laravelを使って階層テーブルを作成する方法は?

id 
theme_id 
description 

まあの作品になります、私が作るしようとしている - 各ジョブのテーマは

id 
parent_id 
name 

worksを保存されます - themes:厚底2つのテーブルを持っています階層テーブル。各行にはテーマがあり、行を開くとテーマの作品が表示され、子テーマがある場合はこれとリンクが表示されます。このような

何か:モデルで hierarchical table

、私が持っている:私のコントローラで

​​

を(私はこれを改善する必要があります):

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-treeviewtreetableを使用してみましたが、多分私の問題は、フロントエンドでの論理です。誰かが私を助けて私に光を与えることができますか?

+0

することができますあなたのコードのいくつかを表示しますか? – Drudge

+0

このチュートリアルを試すhttp://itsolutionstuff.com/post/laravel-5-category-treeview-hierarchical-structure-example-with-demoexample.html – manjunath

+0

再帰的な方法が必要 –

答えて

1

コンポーネント「kalnoy/nestedset」は、ツリー構造を構築するためのコンポーネントであり、必要なすべてのメソッドが含まれています。 あなたがそれを使用します場合は、あなたがこの

Schema::create('items', function (Blueprint $table) { 
    $table->increments('id'); 
    ... 
    NestedSet::columns($table); 
    ... 
}); 

のようにテーブルに指定された列を追加する必要があり、このようなツリー構造を生成するには、このコンポーネントのすべてのメソッドを使用することができます。

$root = Item::findOrFail($rootId); 
$nodes = $root->descendants->toTree($root); 

$traverse = function ($items, $prefix = '-') use (&$traverse) { 
    $html = ''; 
    foreach ($items as $item) { 
     $html .= ... (html structure) 
     $html .= $traverse($item->children, $prefix . '-'); 
    } 
    return $html; 
}; 

$html .= $traverse($nodes); 
関連する問題