如何通过Laravel 5.6错误修复Laravel Group

时间:2019-03-24 12:09:09

标签: laravel

6
面对Laravel Group By Error。 我可以通过更改来解决

'strict' => true, to 'strick'=>false

这是我的SQL查询:

select `employee_id`, `section_id`, `out_time` from `attendances` where `employee_id` is not null and `out_time` is null group by `section_id`

它在sql命令下工作。 当我尝试通过此代码使用laravel运行时,显示错误。
Laravel控制器代码如下:

$attendances = DB::table('attendances')
            ->select('employee_id','section_id','out_time')
            ->where('employee_id', '!=', null)
            ->where('out_time', null)
            ->groupBy('section_id')->get();

运行此代码后,其显示错误,错误如下:

SQLSTATE[42000]: Syntax error or access violation: 1055 'erp.attendances.employee_id' isn't in GROUP BY (SQL: select `employee_id`, `section_id`, `out_time` from `attendances` where `employee_id` is not null and `out_time` is null group by `section_id`)

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

尝试获取作为集合的值,然后将结果分组,即使用Collection's groupBy() method而不是查询生成器。

所以您的查询应该是:

$attendances = DB::table('attendances')
        ->select('employee_id','section_id','out_time')
        ->where('employee_id', '!=', null)
        ->where('out_time', null)
        ->get()
        ->groupBy('section_id');