我有一个看起来像这样的查询,我在其中获取特定位置的各种企业的数据,并且我需要能够判断每个企业都有(或没有)女性员工。
$business = Business::where('location', $location)
->with(['staff'])
->get();
return MiniResource::collection($business);
我的迷你资源如下所示:
return [
'name' => $this->name,
'location' => $this->location,
'staff' => PersonResource::collection($this->whenLoaded('staff')),
];
这是示例响应的样子:
{
"id": 1,
"name": "XYZ Business"
"location": "London",
"staff": [
{
"name": "Abigail",
"gender": "f",
"image": "https://s3.amazonaws.com/xxxx/people/xxxx.png",
"role": "Project Manager",
},
{
"name": "Ben",
"gender": "m",
"image": "https://s3.amazonaws.com/xxxx/people/xxxx.png",
"role": "Chef",
},
]
}
我真的不需要人员数组,我只想检查关系中是否存在女性,然后返回类似于以下内容:
{
"id": 1,
"name": "XYZ Business"
"country": "USA",
"has_female_employee": true;
}
有没有一种雄辩的方法来实现这一目标?
注意:在我的原始代码中,我有更多要查询的关系,但我不得不将这篇文章限制在我的问题范围内。