在laravel 5.4中实现无限级分类,网上资料较少,所以本文就和大家介绍关于在laravel 5.4中实现无限级分类的方法示例,需要的朋友可以参考借鉴,下面来一起看看吧。希望能帮助到大家。
前言
本文主要给大家介绍的是关于laravel 5.4中实现无限级分类的相关内容,分享出来供有需要的朋友们参考学习,下面话不多说,来一起看看详细的介绍吧。
方法如下:
1、建立表
php artisan make:migration create_category_table --create=category
在database/migrations/下找到你的迁移文件
建入:
<?php
use illuminate\support\facades\schema;
use illuminate\database\schema\blueprint;
use illuminate\database\migrations\migration;
class createcategorytable extends migration
{
/**
* run the migrations.
*
* @return void
*/
public function up()
{
schema::create('categorys', function (blueprint $table) {
$table->increments('id');
$table->integer('parent_id');
$table->string('code');
$table->string('name');
$table->string('path');
$table->timestamps();
});
}
/**
* reverse the migrations.
*
* @return void
*/
public function down()
{
schema::dropifexists('categorys');
}
}
php artisan migrate
2、建model 在app/category.php
php artisan make: model category -m
<?php
namespace app;
use illuminate\database\eloquent\model;
class category extends model
{
public function childcategory() {
return $this->hasmany('app\category', 'parent_id', 'id');
}
public function allchildrencategorys()
{
return $this->childcategory()->with('allchildrencategorys');
}
}
3、调用
$categorys = app/category::with('allchildrencategorys')->first();
或
$categorys->allchildrencategorys;
或
$categorys->allchildrencategorys->first()->allchildrencategorys;
相关推荐:
php递归实现无限级分类的开发过程及示例代码
php无限级分类实现方法分析
一个更简单的无限级分类菜单代码
以上就是laravel5.4中无限级分类的实现方法的详细内容。