laravel 5.4中实现无限级分类的方法示例
前言
本文主要给大家介绍的是关于laravel5.4中实现无限级分类的相关内容,分享出来供有需要的朋友们参考学习,下面话不多说,来一起看看详细的介绍吧。
方法如下:
1、建立表
phpartisanmake:migrationcreate_category_table--create=category
在database/migrations/下找到你的迁移文件
建入:
increments('id'); $table->integer('parent_id'); $table->string('code'); $table->string('name'); $table->string('path'); $table->timestamps(); }); } /** *Reversethemigrations. * *@returnvoid */ publicfunctiondown() { Schema::dropIfExists('categorys'); } } phpartisanmigrate
2、建Model在app/Category.php
phpartisanmake:modelCategory-m
hasMany('App\Category','parent_id','id'); } publicfunctionallChildrenCategorys() { return$this->childCategory()->with('allChildrenCategorys'); } }
3、调用
$categorys=App/Category::with('allChildrenCategorys')->first();
或
$categorys->allChildrenCategorys;
或
$categorys->allChildrenCategorys->first()->allChildrenCategorys;
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者使用laravel能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。