Laravel groupBy-如何选择多个列并按一列分组

Laravel groupBy-如何选择多个列并按一列分组,laravel,Laravel,如何选择按一列分组的多个列 有人能帮我解答这个问题吗 $data = DB::table('orders') ->where('store_id',$id) ->select('user_id','username','address','userphoneno', DB::raw('count(*) as total'), DB::raw('SUM(total_amount) as price')) ->groupBy('us

如何选择按一列分组的多个列

有人能帮我解答这个问题吗

 $data = DB::table('orders')
        ->where('store_id',$id)
        ->select('user_id','username','address','userphoneno', DB::raw('count(*) as total'), DB::raw('SUM(total_amount) as price'))
        ->groupBy('userphoneno')
        ->get();

首先,必须在config\database.php中禁用mysql的严格模式

'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', 

那么这个查询应该可以工作了

$data = DB::table('orders')
    ->where('store_id',$id)
    ->select('user_id','username','address','userphoneno', DB::raw('count(*) as total'), DB::raw('SUM(total_amount) as price'))
    ->groupBy('userphoneno')
    ->get();

任何不在groupBy中的选择字段,请使用DB::raw('any_VALUE(fieldName)'))
$data = DB::table('orders')
    ->where('store_id',$id)
    ->select('user_id','username','address','userphoneno', DB::raw('count(*) as total'), DB::raw('SUM(total_amount) as price'))
    ->groupBy('userphoneno')
    ->get();