欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

laravel 5.4中實現(xiàn)無限級分類的方法示例

 更新時間:2017年07月27日 10:43:51   作者:羊爸爸  
最近在工作中遇到一個需求,是要在laravel 5.4中實現(xiàn)無限級分類,但發(fā)現(xiàn)網(wǎng)上這個的資料較少,所以只能自己來實現(xiàn)了,下面這篇文章主要給大家介紹了關(guān)于在laravel 5.4中實現(xiàn)無限級分類的方法示例,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

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

方法如下:

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、調(diào)用

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

$categorys->allChildrenCategorys; 

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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者使用laravel能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論