Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shariff's avatar
Level 51

how to use Group by in laravel

hi,

I am to retrieve data based on the day using groupBy. But I am getting an error like this

syntax error or access violation: 1055

what I have tried

public function index(){

    $ids = Academic::active()->pluck('id');
    if(Auth()->user()->getRoleNames() == '["Parent"]')
    {
        $id = Auth()->user()->Parents_id;
        $parent = ParentName::find($id);
        $students = $parent->students;

        foreach($students as $stu)
        {
            $classid[] = $stu->class_id;
        }

        $timetables = DB::table('time_tables')->whereIn('academic_id', $ids)->whereIn('class', $classid)->groupBy('day')->get();

      //  $timetables = TimeTable::whereIn('academic_id', $ids)->whereIn('class', $classid)->get();

        return response()->json($timetables);


    }
0 likes
8 replies
manelgavalda's avatar

Hey, this is a problem related with the mysql mode.

Go to config\database.php, mysql and set strict to false.

shariff's avatar
Level 51

@manelgavalda I am getting this error

SQLSTATE[42000]: Syntax error or access violation: 1055 'schoolmanagement.time_tables.id' isn't in GROUP BY (SQL: select * from `time_tables` where `academic_id` in (24) and `class` in (2, 4) group by `day`)"
manelgavalda's avatar
Level 50

Mm, if the strict mode is not solving it, you can try to use groupBy after the get to use it as a collection method:

DB::table('time_tables')->whereIn('academic_id', $ids)->whereIn('class', $classid)->get()->groupBy('day');

Please or to participate in this conversation.