laravel 5.4中实现无限级分类的方法示例
前言
本文主要给大家介绍的是关于laravel 5.4中实现无限级分类的相关内容,分享出来供有需要的朋友们参考学习,下面话不多说,来一起看看详细的介绍吧。
方法如下:
1、建立表
php artisan make:migration create_category_table --create=category
在database/migrations/下找到你的迁移文件
建入:
<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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 IlluminateDatabaseEloquentModel;
class Category extends Model
{
public function childCategory() {
return $this->hasMany('AppCategory', '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能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对积木网的支持。
PHP 7安装使用体验之性能大提升,兼容性强,扩展支持不够(升级PHP要谨慎)
伴随着PHP7的发布,这几天关于PHP7性能和兼容性成了大家讨论的热点话题,PHP7表现出来的高性能让不少人蠢蠢欲动,有些尝鲜的朋友已经将PHP7应用到了
浅谈thinkphp5 instance 的简单实现
最近学习ThinkPHP5,第一次看到TestClass::instance()就能创建TestClass实例的方法。感到很好奇,翻阅ThinkPHP的源代码,大体理解了它的设计思想,非常的先进。
PHP将数据导出Excel表中的实例(投机型)
1、简介如何利用最简单粗糙暴力的方法将数据写入Excel文件中呢?因为msword和excel的文档都支持html文本格式,因此我们可以基于这个原理采用html文本格
扫码添加企业微信,预约演示。
