Implementation method of infinite level classification in laravel5.4

小云云
Release: 2023-03-19 19:38:01
Original
1755 people have browsed it

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
Copy after login

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
Copy after login

2. Create Model in app/Category.php

php artisan make: model Category -m
Copy after login
<?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');
 }
}
Copy after login

3. Call

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

or

$categorys->allChildrenCategorys;
Copy after login

Or

$categorys->allChildrenCategorys->first()->allChildrenCategorys;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!