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

Shiva's avatar
Level 5

SELECT list is not in GROUP BY clause and contains nonaggregated column

I'm trying to group my data by date. but I'm getting this error

Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'laravel-site.tech.id' 
which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 
(SQL: select * from `tech` group by `created_at`)

Here is my code

$table = DB::table('tech')
                    ->select('*')
                    ->groupBy('created_at')
                    ->get();
        dd($table);
0 likes
4 replies
sr57's avatar

You can only select grouped filelds

$table = DB::table('tech')
                    ->select('created_at')
                    ->groupBy('created_at')
                    ->get();
marcus_sugarcoated's avatar

Why not group by on the collection once you've fetched the results from the database?

kokoshneta's avatar

@alfrednutile You cannot really compare the two. They do entirely different things. Database grouping cannot achieve the same result as collection grouping can – to wit, you cannot retrieve a full set of complete records grouped by a column, as in the example in the question here, from an SQL database.

That requires post-retrieval grouping. The GROUP BY directive in SQL is really kind of a misnomer; it would have been more accurate if it were AGGREGATE BY, since that’s what it’s made for. The ->groupBy() method in Laravel collections, on the other hand, does true grouping.

2 likes

Please or to participate in this conversation.