前言
本文主要給大家介紹的是關于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;
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者使用laravel能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對的支持。
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。