介绍
今天就跟大家聊聊有关如何在laravel 5.4中实现一个无限级分的类,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
<强> 1,建立表强>
php artisan : migration create_category_table ——创建=
类别在数据库迁移/下找到你的迁移文件
<强>建入:强>
& lt; php ? , use 阐明\ \外墙\模式的支持; use 照亮\数据库\模式\蓝图; use 照亮\迁移\ \数据库迁移; , class CreateCategoryTable  extends 迁移 {/* * ,* Run 从而;迁移。 ,* ,* @return 空白 ,*/,public function () ,{ ,模式:创建(& # 39;思想史范畴# 39;,,function (Blueprint $表),{ 表→美元才能增量(& # 39;id # 39;); 表→美元才能整数(& # 39;parent_id& # 39;); 表→美元才能字符串(& # 39;代码# 39;); 表→美元才能字符串(& # 39;名字# 39;); 表→美元才能字符串(& # 39;path & # 39;); 美元才能表→时间戳(); ,}); ,} ,/* * ,* Reverse 从而;迁移。 ,* ,* @return 空白 ,*/向下,public function () ,{ ,模式::dropIfExists(& # 39;思想史范畴# 39;); ,} } php artisan 迁移
<强> 2,建模型在app/类别。php 强>
php artisan :, model Category - m
& lt; php ? , namespace 应用; , use 阐明\雄辩的\ \数据库模型; , class Category  extends 模型 { ,public function  childCategory (), { ,return 这→美元hasMany(& # 39;应用\类别# 39;,,& # 39;parent_id& # 39;,, & # 39; id # 39;); ,} , ,public function  allChildrenCategorys () ,{ ,return 这→美元childCategory()→与(& # 39;allChildrenCategorys& # 39;); ,} }
<强> 3,调用强>
categorys 美元;=,App/类别::与(& # 39;allChildrenCategorys& # 39;)→第();
或
思想史范畴→美元allChildrenCategorys;
或
思想史范畴→美元allChildrenCategorys→第()→allChildrenCategorys;
看完上述内容,你们对如何在laravel 5.4中实现一个无限级分类有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。