首页 > 解决方案 > 我做了一个分类,想做子分类,怎么动态添加?

问题描述

我制作了一个表格,其中有标题、图像、pdf、内容和类别,一切都运行良好,但我想在我的表格中添加子菜单,也想让这个动态化,以便可以在我的网站上显示所有子菜单。

标签: laravel-5eloquentlaravel-5.2

解决方案


您必须parent_id使用类别表插入一个键。

在类别模型中创建这样的关系。

class Category extends Model
{


    public $fillable = ['title','parent_id'];


    /**
     * Get the index name for the model.
     *
     * @return string
    */
    public function childs() {
        return $this->hasMany('App\Category','parent_id','id') ;
    }
}

在这里你可以找到完整的例子。https://itsolutionstuff.com/post/laravel-5-category-treeview-hierarchical-structure-example-with-demoexample.html

希望这可以帮助你。


推荐阅读