加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 19:54:22 所属栏目:大数据 来源:网络整理
导读:《:laravel 5.4中实现无限级分类的方法示例》要点: 本文介绍了:laravel 5.4中实现无限级分类的方法示例,希望对您有用。如果有疑问,可以联系我们。 前言 PHP实战 本文主要给大家介绍的是关于laravel 5.4中实现无限级分类的相关内容,分享出来供有需要的朋

《:laravel 5.4中实现无限级分类的方法示例》要点:
本文介绍了:laravel 5.4中实现无限级分类的方法示例,希望对您有用。如果有疑问,可以联系我们。

前言PHP实战

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

方法如下:PHP实战

1、建立表
PHP实战

php artisan make:migration create_category_table --create=category

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

建入:PHP实战

<?php
 
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
 
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实战

php artisan make: model Category -m
<?php
 
namespace App;
 
use IlluminateDatabaseEloquentModel;
 
class Category extends Model
{
 public function childCategory() {
 return $this->hasMany('AppCategory','parent_id','id');
 }
 
 public function allChildrenCategorys()
 {
 return $this->childCategory()->with('allChildrenCategorys');
 }
}

3、调用
PHP实战

$categorys = App/Category::with('allChildrenCategorys')->first();

PHP实战

$categorys->allChildrenCategorys; 

PHP实战

$categorys->allChildrenCategorys->first()->allChildrenCategorys;

总结PHP实战

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读