Reputation: 17
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
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