微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

laravel 5.4中实现无限级分类的方法示例

前言

本文主要给大家介绍的是关于laravel 5.4中实现无限级分类的相关内容分享出来供有需要的朋友们参考学习,下面话不多说,来一起看看详细的介绍吧。

方法如下:

1、建立表

rush:PHP;"> PHP artisan make:migration create_category_table --create=category

在database/migrations/下找到你的迁移文件

建入:

rush: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

rush:plain;"> PHP artisan make: model Category -m
rush: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、调用

first();

allChildrenCategorys;

allChildrenCategorys->first()->allChildrenCategorys;

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者使用laravel能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程之家的支持

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。