2016-04-18 3 views
0

に私はこのようなデータベーステーブルを持っている::Cartegoryとサブカテゴリをlaravel 5.2

enter image description here

私はthisのようなカテゴリとサブカテゴリを作りたいです。

私はリンクの手順に従っていますが、私はそのページのmanojsharma20と同じ問題に直面しています。

サブカテゴリもカテゴリとして表示されます。

サブカテゴリをカテゴリとして表示しないようにするにはどうすればよいですか。ここで

enter image description here

+0

編集 –

+0

@jamesおかげでまだスナップごとに示します。 –

答えて

1

は、お使いのモデル名があるとしカテゴリー

  1. パブリック関数のチャイルズ() { 戻ります$ this-> hasManyの( 'アプリケーション\カテゴリ'、 'カテゴリーのモデルに関数を作成parent_id '、' id '); }

  2. $ categories = category :: with( 'childs') - > where( 'parent_id'、0) - > get();

2

は、あなたのモデル名は、あなたがparentとそのchildren

$list = \App\Category::with('cat_childs')->all(); 

//this will show you parent and its children 
echo "<pre>"; 
    print_r($list); 
echo "</pre>"; 
を取得します。このメソッドを使用してCategory

/*--------------------------------------------------------- 
* Relationship with same table, means recursive key 
* -------------------------------------------------------- 
*/ 
//using this relation, you will get parent children 
public function cat_childs(){ 
    return $this->hasMany('App\Category', 'parent_id', 'id'); 
} 

//using this relation, it will tell you, who is the parent of children 
public function cat_parent(){ 
    return $this->belongsTo('App\Category', 'parent_id', 'id'); 
} 

であると仮定し、私の実施例であります

+0

ための[リンク] https://laracasts.com/discuss/channels/general-discussion/categories-and-subcategories?page=1 –