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

surgawa's avatar

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2

hi i got this error when trying to make a chart

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'klinik_bersalin.ibu.created_at' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

and this is my controller

 public function index()
    {
        $ibu = Ibu::select(DB::raw("COUNT(*) as count"), DB::raw("MONTHNAME(created_at) as month_name"))
                    ->whereYear('created_at', date('Y'))
                    ->groupBy(DB::raw("Month(created_at)"))
                    ->pluck('count', 'month_name');

        $labels = $ibu->keys();
        $data = $ibu->values();

        return view('chart', compact('labels', 'data'));
    }

How do i resolve it? Thank you

0 likes
7 replies
JussiMannisto's avatar

@vincent15000 @surgawa This is wrong, don't do it. You're trying to aggregate data by month. Grouping the results by their timestamp would produce nonsense results.

What you need to do instead is group the results by the month name. You can use the alias that you defined for it:

->groupBy('month_name')
1 like

Please or to participate in this conversation.