laravel-5 - 我做了一个分类,想做子分类,怎么动态添加?
问题描述
我制作了一个表格,其中有标题、图像、pdf、内容和类别,一切都运行良好,但我想在我的表格中添加子菜单,也想让这个动态化,以便可以在我的网站上显示所有子菜单。
解决方案
您必须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
希望这可以帮助你。
推荐阅读
- pytorch - 从 TPU 保存的 pytorch 模型在 CPU 上运行
- java - IllegalArgumentException - 休眠错误 - org.hibernate.hql.internal.ast.QuerySyntaxException:Clasa 未映射 [SELECT c FROM Clasa c]
- javascript - 提交和序列化时忽略表单元素
- python - 从 STDIN 读取后,Python 在 `input(...)` 处崩溃
- python - 如何使 ForeignKey 字段使用同一列表中的多个下拉菜单,并在 Django 中删除被选中的重复项?
- sql - 使用名字、姓氏和 WHERE 子句从现有工作表值中查询需要匹配名字和姓氏
- reactjs - 如何在 React 中的两个状态之间制作动画并在转换期间查看这两个状态
- java - 在单个事务中插入多个实体 | 休眠,JPA
- php - HHClient 为 HSL 中的函数抛出未绑定名称错误
- css - CSS Grid:如何跨越父元素宽度的网格单元格?