laravel 5 嵌套类别和嵌套子类别
laravel 5 nested category and nested sub category
使用 Laravel 5.6,我试图显示 MySQL 个类别 table 中子类别的子类别。我想传递名称并获取其所有子类别,而不管父类别如何。
类别Table
id | name | cat_parent_id
--- | ------------------| -------------
1 | Parent - 1 | NULL
2 | Parent - 2 | NULL
3 | Child-1- P - 1 | 1
4 | Child-1- P - 2 | 2
5 | sCh-1-Ch-1-P- 2 | 4
6 | sCh-2-Ch-1-P- 2 | 4
7 | sCh-3-Ch-1-P- 2 | 4
8 | sCh-4-Ch-1-P- 2 | 4
9 | sCh-5-Ch-1-P- 2 | 4
想要的结果
return App\Category::where('name','Child-1- P - 2')->子级->get();
id | name | cat_parent_id
--- | ------------------| -------------
5 | sCh-1-Ch-1-P- 2 | 4
6 | sCh-2-Ch-1-P- 2 | 4
7 | sCh-3-Ch-1-P- 2 | 4
8 | sCh-4-Ch-1-P- 2 | 4
9 | sCh-5-Ch-1-P- 2 | 4
如果我理解得很好,要获得 children
关系,您可以在 App\Category
模型上使用以下方法:
// app/Category.php
public function children(): HasMany
{
return $this->hasMany(static::class, 'cat_parent_id', 'id');
}
然后获取主分类的所有children:
use App\Category;
$children = Category::where('name','Child-1- P - 2')->first()->children;
这里是工厂的支持测试:
// database/factories/CategoryFactory.php
use App\Category;
$factory->define(Category::class, function (Faker $faker) {
static $id = 1;
return [
'name' => 'Category '.$id++,
'cat_parent_id' => null,
];
});
// tests/Unit/Models/CategoryTest.php
use App\Category;
/**
* @test
*/
public function returns_associated_child_records()
{
// create master records
factory(Category::class, 3)->create();
// get parent for the sub-categories
$parent = $master->first();
// create sub-categories
foreach(range(1, 4) as $id) {
factory(Category::class)->create([
'name' => 'Sub category '.$id,
'cat_parent_id' => $parent->id
]);
}
$this->assertEquals(
['Sub category 1', 'Sub category 2', 'Sub category 3', 'Sub category 4'],
Category::where('name', $parent->name)->first()->children->pluck('name')->toArray()
);
}
我在这里工作的前提是类别名称是唯一的 - 否则您将不得不遍历记录集合。
使用 Laravel 5.6,我试图显示 MySQL 个类别 table 中子类别的子类别。我想传递名称并获取其所有子类别,而不管父类别如何。
类别Table
id | name | cat_parent_id
--- | ------------------| -------------
1 | Parent - 1 | NULL
2 | Parent - 2 | NULL
3 | Child-1- P - 1 | 1
4 | Child-1- P - 2 | 2
5 | sCh-1-Ch-1-P- 2 | 4
6 | sCh-2-Ch-1-P- 2 | 4
7 | sCh-3-Ch-1-P- 2 | 4
8 | sCh-4-Ch-1-P- 2 | 4
9 | sCh-5-Ch-1-P- 2 | 4
想要的结果
return App\Category::where('name','Child-1- P - 2')->子级->get();
id | name | cat_parent_id
--- | ------------------| -------------
5 | sCh-1-Ch-1-P- 2 | 4
6 | sCh-2-Ch-1-P- 2 | 4
7 | sCh-3-Ch-1-P- 2 | 4
8 | sCh-4-Ch-1-P- 2 | 4
9 | sCh-5-Ch-1-P- 2 | 4
如果我理解得很好,要获得 children
关系,您可以在 App\Category
模型上使用以下方法:
// app/Category.php
public function children(): HasMany
{
return $this->hasMany(static::class, 'cat_parent_id', 'id');
}
然后获取主分类的所有children:
use App\Category;
$children = Category::where('name','Child-1- P - 2')->first()->children;
这里是工厂的支持测试:
// database/factories/CategoryFactory.php
use App\Category;
$factory->define(Category::class, function (Faker $faker) {
static $id = 1;
return [
'name' => 'Category '.$id++,
'cat_parent_id' => null,
];
});
// tests/Unit/Models/CategoryTest.php
use App\Category;
/**
* @test
*/
public function returns_associated_child_records()
{
// create master records
factory(Category::class, 3)->create();
// get parent for the sub-categories
$parent = $master->first();
// create sub-categories
foreach(range(1, 4) as $id) {
factory(Category::class)->create([
'name' => 'Sub category '.$id,
'cat_parent_id' => $parent->id
]);
}
$this->assertEquals(
['Sub category 1', 'Sub category 2', 'Sub category 3', 'Sub category 4'],
Category::where('name', $parent->name)->first()->children->pluck('name')->toArray()
);
}
我在这里工作的前提是类别名称是唯一的 - 否则您将不得不遍历记录集合。