【发布时间】:2020-05-31 20:57:30
【问题描述】:
我要计算来自同一个国家的注册数量。 到目前为止,我提出了以下解决方案: 控制器内部方法:
class Camp extends Model // ...
{
protected $with = ['registrations_nation_count',...];
// ...
public function registrations_nation_count()
{
return $this->hasMany(CampRegistration::class)
->select(['camp_id','nation',DB::raw('COUNT(*) as count')])
->groupBy('nation');
}
}
这会生成以下输出:
"registrations_nation_count": [
{
"camp_id": 1,
"nation": "en",
"count": 2
},
{
"camp_id": 1,
"nation": "fr",
"count": 1
}
]
现在我想从输出中删除 camp_id 列。我已经尝试简单地将其从选择中删除,但这根本不会导致输出。 有什么办法可以隐藏吗?
数据库:
+-------+--------+ |营地 | camp_registrations | +-------+--------+ |编号 |编号 | | ... | camp_id | | |民族| | | ... | +-------+--------+【问题讨论】:
标签: php sql laravel eloquent relationship