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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

羊爸爸   2021-03-24 我要评论
想了解laravel 5.4中实现无限级分类的方法示例的相关内容吗,羊爸爸在本文为您仔细讲解laravel 5.4无限级分类的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:laravel无限级分类,laravel,无限分类,laravel,无限极分类,下面大家一起来学习吧。

前言

本文主要给大家介绍的是关于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;

总结

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

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们