前言
本文主要给大家介绍的是关于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能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
利用Homestead快速运行一个Laravel项目的方法详解这篇文章主要给大家介绍了关于利用Homestead如何快速运行一个Laravel项目的相关资料,文中通过示例代码介绍的非常详
Laravel Intervention/image图片处理扩展包的安装、使用与可能遇到的坑详解这篇文章主要给大家介绍了关于Laravel中Intervention/image图片处理扩展包的安装、使用与在使用可能遇到的坑的解决方法
Laravel下生成验证码的类这篇文章主要为大家详细介绍了Laravel下生成验证码的类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
详解php语言最牛掰的Laravel框架Laravel以其简洁、优雅的特性赢得了大家的广泛关注,无论是专家还是新手,在开发PHP项目的时候,都会第一时间的想
Laravel 批量更新多条数据的示例本篇文章主要介绍了Laravel 批量更新多条数据的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起
laravel5 使用try catch的实例详解这篇文章主要介绍了laravel5 使用try catch的相关知识,需要的朋友可以参考下