Home  >  Article  >  Backend Development  >  Implementation method of infinite level classification in laravel5.4

Implementation method of infinite level classification in laravel5.4

小云云
小云云Original
2018-02-01 10:12:551756browse

There are few online materials to implement infinite classification in laravel 5.4, so this article will introduce to you examples of methods to implement infinite classification in laravel 5.4. Friends who need it can refer to it. Let’s take a look together. Hope it helps everyone.

Preface

This article mainly introduces to you the relevant content about the implementation of infinite classification in laravel 5.4. It is shared for reference and learning by friends in need. There is not much to say below. Let’s take a look at the detailed introduction.

The method is as follows:

1. Create a table

php artisan make:migration create_category_table --create=category

Find your migration file under database/migrations/

Create Enter:

<?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(&#39;categorys&#39;, 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. Create Model in 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. Call

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

or

$categorys->allChildrenCategorys;

Or

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

Related recommendations:

php development process and sample code for recursively implementing infinite level classification

php unlimited level Analysis of classification implementation methods

A simpler infinite-level classification menu code

The above is the detailed content of Implementation method of infinite level classification in laravel5.4. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn