>  기사  >  백엔드 개발  >  laravel5.4의 무한 수준 분류 구현 방법

laravel5.4의 무한 수준 분류 구현 방법

小云云
小云云원래의
2018-02-01 10:12:551759검색

laravel 5.4에서는 무한 분류를 구현하기 위한 온라인 자료가 거의 없기 때문에 이번 글에서는 laravel 5.4에서 무한 분류를 구현하는 방법의 예를 소개하겠습니다. 필요한 친구들이 참고하면 됩니다. 그것이 모두에게 도움이 되기를 바랍니다.

머리말

이 글은 라라벨 5.4의 무한 분류 구현과 관련된 내용을 주로 소개합니다. 도움이 필요한 친구들의 참고와 학습을 위해 공유합니다. 아래에서는 자세히 설명하지 않겠습니다. 상세한 소개.

방법은 다음과 같습니다.

1. 테이블을 생성합니다.

php artisan make:migration create_category_table --create=category

database/migrations/

Build에서 마이그레이션 파일을 찾습니다.

<?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.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 ,

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

또는

$categorys->allChildrenCategorys;

또는

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

로 전화하세요. 관련 권장 사항:

php 개발 프로세스의 재귀 구현 및 무한 수준 분류의 샘플 코드

php 무한 수준 분류 구현 방법 분석

단순한 무한 레벨 카테고리 메뉴 코드 하나 더

위 내용은 laravel5.4의 무한 수준 분류 구현 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.