laravel5.4中無限級分類的實現方法

來源:互聯網
上載者:User
在laravel 5.4中實現無限級分類,網上資料較少,所以本文就和大家介紹關於在laravel 5.4中實現無限級分類的方法樣本,需要的朋友可以參考借鑒,下面來一起看看吧。希望能協助到大家。

前言

本文主要給大家介紹的是關於laravel 5.4中實現無限級分類的相關內容,分享出來供有需要的朋友們參考學習,下面話不多說,來一起看看詳細的介紹吧。

方法如下:

1、建立表

php artisan make:migration create_category_table --create=category

在database/migrations/下找到你的遷移檔案

建入:

<?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、建Model 在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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.