Home >Backend Development >PHP Tutorial >About the method of implementing infinite classification in laravel 5.4
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('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. 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;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!