1、建立表
php artisan make:migration create_category_table --create=category
在database/migrations/下找到你的迁移文件
建入:
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 migrate2、建Model 在app/Category.php
php artisan make: model Category -m
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;