i have chat_msgs table it contains (
body, type, from_id, to_id,
)
i want to group by from_id,
my Eloquent
public function index()
{
$chatMsgs=ChatMsg::select('id','body','from_id','to_id')
->where('to_id',Auth()->user()->id)
->with('user:id,name')
->groupBy('from_id')
->get();
return $chatMsgs;
}
but I got this error
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'mhpss.chat_msgs.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select id, body, from_id, to_id from chat_msgs where to_id = 1 group by from_id)
I try to use
public function index()
{
$chatMsgs=ChatMsg::select('id','body','from_id','to_id')
->where('to_id',Auth()->user()->id)
->with('user:id,name')
->groupBy('from_id','id')
->get();
return $chatMsgs;
}
but I got all record without groubing