Вот мой рабочий пример,
Пусть ваше имя Модель 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');
}
так с помощью этого метода вы получите parent
и его children
$list = \App\Category::with('cat_childs')->all();
//this will show you parent and its children
echo "<pre>";
print_r($list);
echo "</pre>";
[ссылка] https://laracasts.com/discuss/channels/general-discussion/categories-and-subcategories?page=1 –
@james спасибо за редактирование –