從這個問題:How to GROUP and SUM a pivot table column in Eloquent relationship?Laravel - 雄辯關係belongsToMany GROUPBY「語法錯誤或訪問衝突」錯誤
我在User
型號使用此代碼:
public function teams() {
return $this->belongsToMany('App\Models\Team', 'team_members', 'user_id', 'team_id')
->groupBy('pivot_team_id');
}
我想用->groupBy()
因爲在我team
s,單個user
可以充當不同角色的多個team_member
。我不想team
記錄重複。
但是當我嘗試訪問例如我dd(Auth::user()->teams)
網頁上的代碼,Laravel發生以下異常:
SQLSTATE[42000]: Syntax error or access violation: 1055 'laravel.teams.id' isn't in GROUP BY (SQL: select `teams`.*, `team_members`.`user_id` as `pivot_user_id`, `team_members`.`team_id` as `pivot_team_id` from `teams` inner join `team_members` on `teams`.`id` = `team_members`.`team_id` where `team_members`.`user_id` = 3 group by `pivot_team_id`)
這是爲什麼呢?我哪裏弄錯了?
看看這是否有幫助 - https://github.com/barryvdh/laravel-translation-manager/issues/144 – ayip
@ayip現在確實有效,但它看起來像一個黑客攻擊。必須有辦法做到這一點,而不必禁用整個組 –