如果子类别中有产品,我想统计该子类别的父类别中的所有产品。 例如:
类别1(总产品10)//我如何获得它
类别1.1(总产品5)
类别1.2(总产品3)
类别1.3(总产品2)
Json代码:
id: 291,
level: 1,
parent_id: null, //parent category
name: "Chemicals",
bn_name: null,
sector: "product",
slug: "Chemicals",
status: "active",
created_at: "2021-04-16T23:53:52.000000Z",
updated_at: "2021-04-16T23:53:52.000000Z",
**products_count: 0,**
total_items: 1,
sub_categories: [
id: 292,
level: 2,
parent_id: 291,
name: "Activated Carbon",
bn_name: null,
sector: "product",
slug: "Activated-Carbon",
status: "active",
created_at: "2021-04-16T23:53:52.000000Z",
updated_at: "2021-04-16T23:53:52.000000Z",
products_count: 0,
total_items: 0,
sub_categories: [ ]
},
{
id: 293,
level: 2,
parent_id: 291,
name: "Adhesives & Sealants",
bn_name: null,
sector: "product",
slug: "Adhesives-&-Sealants",
status: "active",
created_at: "2021-04-16T23:53:52.000000Z",
updated_at: "2021-04-16T23:53:52.000000Z",
products_count: 0,
total_items: 0,
sub_categories: [ ]
},
{
id: 294,
level: 2,
parent_id: 291,
name: "Agro Chemicals", //child category
bn_name: null,
sector: "product",
slug: "Agro-Chemicals",
status: "active",
created_at: "2021-04-16T23:53:52.000000Z",
updated_at: "2021-04-16T23:53:52.000000Z",
**products_count: 1,**
total_items: 1,
sub_categories: [ ]
}
控制器代码是:
return $categories = Category::rootCategories()->with('subCategories')->withCount('products')->orderBy('name', 'ASC')->get();
类别型号:
public static function rootCategories() {
return self::whereNull('parent_id')->where('status', 'active')->orderBy('name', 'ASC');
}
public static function productRootCategories() {
return self::whereNull('parent_id')->where(['status' => 'active', 'sector' => 'product'])->orderBy('name', 'asc');
}
public function subCategories() {
return $this->hasMany(self::class, 'parent_id')->where('status', 'active')->with('subCategories')->withCount('products')->orderBy('name', 'asc');
}
您可以通过两种方式做到这一点。
只需在产品表中添加category_id 列稍微更改一下数据库关系,并在添加新产品时指定类别即可。这是最简单、最容易的方法,因为当你想统计一个类别的所有产品时,只需在类别模型中添加 hasMany 函数即可轻松获得 -
public function products()
{
return $this->hasMany(Product::class);
}
并在产品模型中添加belongsTo函数 -
public function category()
{
return $this->belongsTo(Category::class);
}
您已设置完毕,现在像这样调用控制器 -
use App\Models\Category;
$category = Category::find(your_category_id);
$category_products = count($category->products);
就完成了,简单。
如果你的产品表中没有类别列,并且你已经在产品表中添加了大量数据,那么使用 eloquent collection 和 foreach 循环来计算产品-
首先在子类别的类别模型中添加 hasMany 函数 -
public function subCategories()
{
return $this->hasMany(SubCategory::class);
}
然后是类别的子类别模型中的logsTo函数 -
public function category()
{
return $this->belongsTo(Category::class);
}
并在 SubCategory 中为定义 subcategory_id 的产品添加 hasMany 函数 -
public function products()
{
return $this->hasMany(Product::class, 'subcategory_id');
}
完成!现在在控制器中使用此方法来按您的类别获取产品并对其进行计数。
$category = Category::findOrFail(your_category_id);
$result = array();
foreach ($category->subCategories as $key => $sub) {
array_push($result, $sub->products->toArray());
}
$products = call_user_func_array('array_merge', $result);
return count($products);
如果您还想获取所有类别数据,请像这样使用 -
$data = array();
$categories = Category::all();
foreach ($categories as $category) {
$result = array();
foreach ($category->subCategories as $key => $sub) {
array_push($result, $sub->products->toArray());
}
$products = call_user_func_array('array_merge', $result);
$data[$category->id] = count($products);
}
return $data;
现在您将获得产品总数:)