Home  >  Article  >  Backend Development  >  About the method of implementing infinite classification in laravel 5.4

About the method of implementing infinite classification in laravel 5.4

不言
不言Original
2018-06-13 13:46:511183browse

This article mainly introduces the method of realizing unlimited classification in laravel 5.4. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

I encountered it at work recently One requirement is to implement unlimited classification in laravel 5.4, but I found that there is less information on this online, so I can only implement it by myself. The following article mainly introduces you to the implementation of infinite classification in laravel 5.4. Friends who need it can refer to the method examples. Let’s take a look below.

Preface

This article mainly introduces to you the relevant content about the implementation of infinite classification in laravel 5.4, and shares it for those who need it Friends, please refer to it for reference. I won’t say much 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/

Build in:

<?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(&#39;id&#39;);
  $table->integer(&#39;parent_id&#39;);
  $table->string(&#39;code&#39;);
  $table->string(&#39;name&#39;);
  $table->string(&#39;path&#39;);
  $table->timestamps();
 });
 }
 
 /**
 * Reverse the migrations.
 *
 * @return void
 */
 public function down()
 {
 Schema::dropIfExists(&#39;categorys&#39;);
 }
}
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(&#39;App\Category&#39;, &#39;parent_id&#39;, &#39;id&#39;);
 }
 
 public function allChildrenCategorys()
 {
 return $this->childCategory()->with(&#39;allChildrenCategorys&#39;);
 }
}

3. Call

$categorys = App/Category::with(&#39;allChildrenCategorys&#39;)->first();

or

$categorys->allChildrenCategorys;

or

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

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please Follow PHP Chinese website!

Related recommendations:

About the implementation method of rewriting resource routing custom URL in Laravel

About Laravel queue Implementation principles and how to solve problems

About the use of cookies in Laravel5

The above is the detailed content of About the method of implementing infinite classification in laravel 5.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