About the method of implementing infinite classification in laravel 5.4

不言
Release: 2023-03-31 22:30:02
Original
1241 people have browsed it

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

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
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(&#39;App\Category&#39;, &#39;parent_id&#39;, &#39;id&#39;);
 }
 
 public function allChildrenCategorys()
 {
 return $this->childCategory()->with(&#39;allChildrenCategorys&#39;);
 }
}
Copy after login

3. Call

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

or

$categorys->allChildrenCategorys;
Copy after login

or

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

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!

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