一起源码网

标题: laravel5.4中无限级分类的实现方法 [打印本页]

作者: 云文章    时间: 2020-6-12 12:30
标题: laravel5.4中无限级分类的实现方法
在laravel 5.4中实现无限级分类,网上资料较少,所以本文就和大家介绍关于在laravel 5.4中实现无限级分类的方法示例,需要的朋友可以参考借鉴,下面来一起看看吧。希望能帮助到大家。

前言

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

方法如下:

1、建立表

php artisan make:migration create_category_table --create=category

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

建入:

<?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 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、调用

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

$categorys->allChildrenCategorys;

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



作者: z1z2z3as    时间: 2022-10-20 12:36
java经典源代码




欢迎光临 一起源码网 (https://www.171739.xyz/) Powered by Discuz! X3.3