2015-08-25 74 views
4

對於Laravel來說相當新穎,我正在爲我的應用程序創建一個樹狀分類結構。這是我用過的代碼,但仍然無法實現我想要的。 我的控制器:在Laravel 5中創建一個嵌套的類別列表

public function index() 
{ 
    $categories = Category::with('children')->get(); 

    return view('backend.categories.index')->with('categories', $categories); 
} 

我的類模型:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Category extends Model 
{ 
    protected $guarded = ['id']; 

    public function parent() 
    { 
     return $this->belongsTo('App\Category', 'parent_id'); 
    } 

    public function children() 
    { 
     return $this->hasMany('App\Category', 'parent_id'); 
    } 
} 

我的觀點:

<table class="table table-bordered table-hover"> 
       <thead> 
        <tr> 
         <th>Name</th> 
         <th>Description</th> 
         <th>Slug</th> 
         <th>Action</th> 
        </tr> 
       </thead> 
       <tbody> 
       @foreach ($categories as $category) 
        {{[email protected] ($category->children as $children)--}} 
        <tr> 
         <td>{{ $category->name }}</td> 
         <td>{{ $category->description }}</td> 
         <td>{{ $category->slug }}</td> 
         <td><a class="edit" href="{!! action('Admin\[email protected]', $category->id) !!}" title="Edit"><i class="fa fa-pencil-square-o"></a></i> <a class="delete" href="{!! action('Admin\[email protected]', $category->id) !!}" title="Are you sure you want to delete?"><i class="fa fa-trash-o"></i></a></td> 
         @foreach ($category->children as $children) 
          <tr> 
           <td>{{ $children->name }}</td> 
           <td>{{ $children->description }}</td> 
           <td>{{ $children->slug }}</td> 
           <td></td> 
          </tr> 
         @endforeach 
        </tr> 
       </tbody> 
        {{[email protected]}} 
       @endforeach 
      </table> 

正在試圖製造類似下面的結構:

  • 服飾
  • 手機
    • 智能手機

編輯 有我的數據庫結構:

+-------------+------------------+------+-----+---------------------+----------------+ 
| Field  | Type    | Null | Key | Default    | Extra   | 
+-------------+------------------+------+-----+---------------------+----------------+ 
| id   | int(10) unsigned | NO | PRI | NULL    | auto_increment | 
| name  | varchar(255)  | NO |  | NULL    |    | 
| slug  | varchar(255)  | NO |  | NULL    |    | 
| parent_id | int(11)   | YES |  | NULL    |    | 
| description | text    | NO |  | NULL    |    | 
| created_at | timestamp  | NO |  | 0000-00-00 00:00:00 |    | 
| updated_at | timestamp  | NO |  | 0000-00-00 00:00:00 |    | 
+-------------+------------------+------+-----+---------------------+----------------+ 
+0

您正在載入分類數據。嘗試--- $ categories = Category :: with(['parent','children']) - > get(); – user4621032

+0

真的只是加載孩子,但如果我加載父母和孩子,我如何在我的視圖中使用它。對不起Laravel – ammezie

回答

1

您正在載入的所有類別(包括孩子的),然後: - <i class="fa fa-pencil-square-o"></i></a>

然後在視圖

$categories = Category::with(['children'])->get(); 

並糾正標籤:兒童喜歡這個ES循環他們。你只想加載根類別(沒有父類的類別)。爲此,請將控制器方法更改爲僅加載parent_idnull的類別。

$categories = Category::whereNull('parent_id')->with('children')->get(); 
+0

我如何循環瀏覽我的視圖文件 – ammezie

+0

完美!謝謝,這完全符合我想要的方式。 – ammezie

+0

我現在面對如何產生類別編輯窗體中的類別的選擇菜單...在這方面的任何幫助? – ammezie

0

大概在模型則需要更改序指兒童模式:

public function children() 
    { 
     return $this->hasMany('App\Children', 'parent_id'); 
    } 

模型類別:

class Category extends Model 
{ 
    protected $guarded = ['id']; 
    protected $fillable = array('name','description','slug','created_at','updated_at'); 

    public function children() 
    { 
     return $this->hasMany('App\Children', 'parent_id'); 
    } 
} 

模型兒童:

class Children extends Model { 
    protected $table = 'children'; 
    protected $fillable = array('name','description','parent_id','created_at','updated_at'); 
} 

在控制器中獲取類別

@foreach ($categories as $category)    
     <ul> 
      <li>{{ $category->name }}</li> 
      <li>{{ $category->description }}</li> 
      <li>{{ $category->slug }}</li> 
      <li><a class="edit" href="{!! action('Admin\[email protected]', $category->id) !!}" title="Edit"><i class="fa fa-pencil-square-o"></i></a> <a class="delete" href="{!! action('Admin\[email protected]', $category->id) !!}" title="Are you sure you want to delete?"><i class="fa fa-trash-o"></i></a></li> 


       @foreach ($category->children as $children) 
        <ul> 
        <li>{{ $children->name }}</li> 
        <li>{{ $children->description }}</li> 
        <li>{{ $children->slug }}</li> 
        <li></li> 
        </ul> 
       @endforeach 

     </ul> 
@endforeach 
+0

嘗試它,但兒童類別仍然列爲獨立(即父母) – ammezie

+0

你能解釋---仍然列爲獨立 – user4621032

+0

你有一個表兒童的模型嗎?是否有列表中的兒童指的是表格類別(辦公鑰匙) – user4621032

相關問題