模型 Category
:
public function product()
{
return $this->hasMany(products::class);
}
模型 product
:
public function category()
{
return $this->belongsTo(Category::class);
}
我在控制器中处理:
$result = Category::select(['id','name'])
->with(['product:category_id,status'])
->where('order',1)
->get();
打印原始数据时的结果:
[
'id' => 1,'name' => 'Hot'
'product' => [
'status' => 1,'category_id' => 1
]
]
[
'id' => 2,'name' => 'New'
'product' => [
'status' => 2,'category_id' => 2
]
]
..........
我得到了类别 id
和 name
的列表,并根据关系得到了产品数据数组。在我的产品表中,有一个 status
列,其值等于 1,2,3。
现在我想通过使用该关系获得的产品数组计算有多少个 status = 1 和多少个 status = [2,3]?