SQLSTATE[42000]:语法错误或访问冲突:在 Laravel 中使用具有 hasManyThrough 关系的 groupBy 时为 1055

SQLSTATE[42000]: Syntax error or access violation: 1055 when using groupBy with hasManyThrough relation in Laravel

在 hasMany 关系上使用 groupBy 时它工作正常但是 在 hasManythrough 关系上使用 groupBy 时 在我的控制器中:

public function main()
{
    # code...
    $user = Auth::user();
    $results = [
                'vacations' => $user->vacations()->groupBy('type')->selectRaw('sum(days) as days,sum(mins) as mins, type')->get(), //works fine
                'vactionRegisterd' => $user->vacationsRegisterd()->groupBy('type')->selectRaw('sum(days) as days,sum(mins) as mins, type')->get(), //works fine
                'empVacations'=>$user->empVacations, //work fine
                'employees_vacations'=>$user->empVacations()->groupBy('type')->selectRaw('sum(days) as days,sum(mins) as mins, type')->get(),//gives an error 
               ];
    return view('dashboard.dashboard', ['results' => $results]);
}

我有这个错误:

SQLSTATE[42000]: Syntax error or access violation: 1055 'hrweb.users.manager_id' isn't in GROUP BY (SQL: select sum(days) as days,sum(mins) as mins, type, users.manager_id as laravel_through_key from vacations inner join users on users.id = vacations.user_num where users.manager_id = 5 group by type)I have tow tables users ,vacations

我不知道为什么使用 (users.manager_id as laravel through key) 即使我没有使用 "users.manager_id”与“groupBy”或“selectRow”方法。

我在用户模型中的代码:

1- 用户有很多员工,其中员工也是具有不同角色且具有 manager_id != null [user->employees]

的用户
/**
 * Get all of the employees from user table for the current admin User
 */
public function employees()
{
    return $this->hasMany('App\Models\User', 'manager_id');
}

2- 用户有很多假期 [user->vacations]

/**
 * Get all of the vacations for the User
 */
public function vacations()
{
    return $this->hasMany('App\Models\Vacations', 'user_num', 'id');
}

3- 用户有许多假期 [user->employees->vacation]

 /**
 * Get all of the employees vacations for the User
 */
public function empVacations()
{
    return $this->hasManyThrough(Vacations::class, User::class, 'manager_id', 'user_num', 'id', 'id');
}

通过再次阅读错误,我注意到 Laravel 在查询中以某种方式将外键用作 laravel_through_key

通过在 groupBy() 方法中添加 laravel_through_key 解决的问题是这样的:

[...
 'employees_vacations'=>$user->empVacations()
       -> groupBy('laravel_through_key','user_num','type')
       -> selectRaw('sum(days) as days,sum(mins) as mins,user_num, type')->get()
 ]

但我仍然认为有更好的解决方案所以我很乐意知道你的答案^_^