《PHP應用:laravel 5.4中實現無限級分類的方法示例》要點:
本文介紹了PHP應用:laravel 5.4中實現無限級分類的方法示例,希望對您有用。如果有疑問,可以聯系我們。
前言PHP實戰
本文主要給大家介紹的是關于laravel 5.4中實現無限級分類的相關內容,分享出來供有需要的朋友們參考學習,下面話不多說,來一起看看詳細的介紹吧.PHP實戰
方法如下:PHP實戰
1、建立表
PHP實戰
php artisan make:migration create_category_table --create=category
在database/migrations/下找到你的遷移文件
PHP實戰
建入:PHP實戰
<?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實戰
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、調用
PHP實戰
$categorys = App/Category::with('allChildrenCategorys')->first();
或PHP實戰
$categorys->allChildrenCategorys;
或PHP實戰
$categorys->allChildrenCategorys->first()->allChildrenCategorys;
總結PHP實戰
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者使用laravel能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對維易PHP的支持.
PHP實戰
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/417.html