【发布时间】:2022-12-05 20:06:14
【问题描述】:
如何对具有 withPivot() 值的 belongsToMany() 关系的结果进行分组?
groupBy() 会产生 SQL 错误,如果没有 withPivot() 数据,distinct() 会起作用。
但我需要结果中的数据透视表。
// Relationship: room to chores
public function chores(){
return $this->belongsToMany(
Chore::class,
'maps'
)
->withPivot('id', 'room_id', 'chore_id', 'person_id')
->groupBy('chores.id');
}
错误
SELECT list is not in GROUP BY clause and contains nonaggregated column 'pivot.maps.room_id' which is not functionally dependent on columns in GROUP BY clause
【问题讨论】: