How to use Laravel Eloquent with groupBy?
This is my code but I got errors all the time:
Message::whereColumn('host_id', 'sender_id') ->groupBy('host_id') ->orderBy('created_at', 'DESC') ->paginate(10); The error message is:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'project_db.messages.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
messageswherehost_id=sender_idgroup byhost_idorder bycreated_atdesc limit 10 offset 0)
What I'm trying to achieve is display messages as branches (groups), threads.
->groupBy(['host_id', 'sender_id'])sender_idis a non aggregate function Either use it in group by clause or don't use strict mode by addingstrict => falseinconfig/database.phpfilewhereColumn()likeselectandwherefunctions.LikeMessage::select('host_id, sender_id'')->where('condition')GROUP BYchanged. There is more info here: stackoverflow.com/questions/34115174/…