Ripon Uddin
Ripon Uddin

Reputation: 714

How to fix Laravel Group by error in Laravel 5.6

6
Am facing Laravel Group By Error. I can solve this by change

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

Here is my sql query :

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

its working at sql command. When i tried to run with laravel by this code its showing error .
Laravel Controller Code Below :

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

After run this code its showing error, error below :

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`)

How to solve this ?

Upvotes: 1

Views: 274

Answers (1)

senty
senty

Reputation: 12847

Try getting the values as collection and then grouping the results, i.e use Collection's groupBy() method instead of query builder.

So your queries should be:

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

Upvotes: 1

Related Questions