Akshay Ashokan
Akshay Ashokan

Reputation: 17

Laravel groupBy - how to select multiple columns with grouped by one column

how to select multiple columns grouped by one column,

Can someone help me with this query?

 $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();

Upvotes: 0

Views: 3070

Answers (1)

OMR
OMR

Reputation: 12218

first you must disable strict mode for your mysql in config\database.php

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

        'strict' => true,   *// **make if false***

then this query should work.

$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();

Upvotes: 1

Related Questions