正确输出 laravel 中多个表的分组数据
Correctly output grouped data from multiple tables in laravel
一个集合中所有数据的输出有问题。
有 3 tables 示例:类别,products_group,产品。
我尝试通过 lanks link tables 但由于产品 table 可以没有组,所以情况并非如此。
迁移
categories
-id
-title
...
products_group
-id
-title
...
products
-id
-title
-category_id
-product_group_id
...
型号
category
public function products()
{
return $this->hasMany(Product::class, 'category_id');
}
products_group
public function products()
{
return $this->hasMany(
Product::class
)->whereNull('product_group_id');
}
没有它的产品可以在一个组中。
如何发出请求以生成此格式的响应:
$result = [
[
'category_id' => 1,
'title' => 'Category 1',
'items' => [
[
'id' => 1,
'title' => 'Product group 1',
'products' => [
[
'title' => 'product 1',
'product_group_id' => 1,
'...'
],
[
'title' => 'product 2',
'product_group_id' => 1,
'...'
]
]
],
[
'id' => 2,
'title' => 'Product group 2',
'products' => [
[
'title' => 'product 1',
'product_group_id' => 1,
'...'
],
[
'title' => 'product 2',
'product_group_id' => 1,
'...'
]
]
]
]
],
[
'category_id' => 2,
'title' => 'Category 2',
'items' => [
[
'title' => 'product 3',
'product_group_id' => null,
'...'
],
[
'title' => 'product 4',
'product_group_id' => null,
'...'
]
]
],
[...]
];
不是怎么形成这一切的。
@Donkarnash This is clear I mean that in those categories where products are grouped they are displayed normally but also the same products are displayed in the products array the same category
你可以在预先加载关系时有一个约束
$categories = Category::with([
'product_groups.products',
'products' => function($query){
$query->whereNull('product_group_id');
}
)->get();
这将预先加载所有 ProductGroup 记录以及 ProductGroup 中的相关产品记录,以及与类别关联但不与任何 ProductGroup 关联的产品记录。
上面的查询假设你有一个主元 table category_product_group
和 belongsToMany 关系在两边 Category 和 ProductGroup
//category_product_group
- category_id
- product_group_id
//with composite primary key of category_id & product_group_id
关系
//Category.php
public function product_groups()
{
return $this->belongsToMany(ProductGroup::class);
}
//ProductGroup.php
public function categories()
{
return $this->belongsToMany(Category::class);
}
一个集合中所有数据的输出有问题。 有 3 tables 示例:类别,products_group,产品。 我尝试通过 lanks link tables 但由于产品 table 可以没有组,所以情况并非如此。
迁移
categories
-id
-title
...
products_group
-id
-title
...
products
-id
-title
-category_id
-product_group_id
...
型号
category
public function products()
{
return $this->hasMany(Product::class, 'category_id');
}
products_group
public function products()
{
return $this->hasMany(
Product::class
)->whereNull('product_group_id');
}
没有它的产品可以在一个组中。 如何发出请求以生成此格式的响应:
$result = [
[
'category_id' => 1,
'title' => 'Category 1',
'items' => [
[
'id' => 1,
'title' => 'Product group 1',
'products' => [
[
'title' => 'product 1',
'product_group_id' => 1,
'...'
],
[
'title' => 'product 2',
'product_group_id' => 1,
'...'
]
]
],
[
'id' => 2,
'title' => 'Product group 2',
'products' => [
[
'title' => 'product 1',
'product_group_id' => 1,
'...'
],
[
'title' => 'product 2',
'product_group_id' => 1,
'...'
]
]
]
]
],
[
'category_id' => 2,
'title' => 'Category 2',
'items' => [
[
'title' => 'product 3',
'product_group_id' => null,
'...'
],
[
'title' => 'product 4',
'product_group_id' => null,
'...'
]
]
],
[...]
];
不是怎么形成这一切的。
@Donkarnash This is clear I mean that in those categories where products are grouped they are displayed normally but also the same products are displayed in the products array the same category
你可以在预先加载关系时有一个约束
$categories = Category::with([
'product_groups.products',
'products' => function($query){
$query->whereNull('product_group_id');
}
)->get();
这将预先加载所有 ProductGroup 记录以及 ProductGroup 中的相关产品记录,以及与类别关联但不与任何 ProductGroup 关联的产品记录。
上面的查询假设你有一个主元 table category_product_group
和 belongsToMany 关系在两边 Category 和 ProductGroup
//category_product_group
- category_id
- product_group_id
//with composite primary key of category_id & product_group_id
关系
//Category.php
public function product_groups()
{
return $this->belongsToMany(ProductGroup::class);
}
//ProductGroup.php
public function categories()
{
return $this->belongsToMany(Category::class);
}