Answer the question
In order to leave comments, you need to log in
Laravel, grouping via groupby(). How to return an array containing id?
Good afternoon.
There is a query $ret = User::select('id')->groupBy('age')->get();
Groups norms, but you need to get arrays with user ids as a result: [1,3,8,9],[2,4,5,7],[6,10].....
Tell me how to do this. Thank you!
Answer the question
In order to leave comments, you need to log in
$users = User::select(DB::raw('GROUP_CONCAT(id) as id'))->groupBy('age')->get();
$groups = $users->map(function ($item, $key) {
return explode(',', $item['id']); // Возможно здесь ошибся
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question