laravel通过关联模型实现无限极分类

Laravel 通过关联模型实现无限极分类

这个内容是在 Laravel-China 论坛上看到的,怕以后不好找,这里记录一下。原文地址为:https://learnku.com/articles/14068/simple-practice-of-laravel-infinite-class-classification

数据库结构

这里使用省市区结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

class CreateAreasTable extends Migration
{
.
.
.

public function up()
{
Schema::create('areas', function (Blueprint $table) {
$table->unsignedInteger('id');
$table->string('name')->comment('城市名称');
$table->unsignedInteger('pid')->default(0)->comment(
class CreateAreasTable extends Migration
{
.
.
.

public function up()
{
Schema::create('areas', function (Blueprint $table) {
$table->unsignedInteger('id');
$table->string('name')->comment('城市名称');
$table->unsignedInteger('pid')->default(0)->comment('父级id');
});
}
.
.
.
}

Area 模型增加关联方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

class Area extends Model
{
.
.
.

public function childArea() {
return $this->hasMany(Area::class,'pid','id');
}

public function allChildArea()
{
return $this->childArea()->with(
class Area extends Model
{
.
.
.

public function childArea() {
return $this->hasMany(Area::class,'pid','id');
}

public function allChildArea()
{
return $this->childArea()->with('allChildArea');
}

.
.
.

}

测试

通过如下代码可以得到所有地区的无限极分类结构。更改条件可以查看某个地区及其子地区的无限极分类结构

1
2
3
4

$res = Area::with('allChildArea')->where('pid',0)->get();
return
$res = Area::with('allChildArea')->where('pid',0)->get();
return $res;


laravel通过关联模型实现无限极分类
http://blog.xiangdangnian.net.cn/2020/01/15/laravel通过关联模型实现无限极分类/
作者
chenggx
发布于
2020年1月15日
许可协议