Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 分组后计数错误_Laravel_Builder - Fatal编程技术网

Laravel 分组后计数错误

Laravel 分组后计数错误,laravel,builder,Laravel,Builder,有带钱包的桌子和服务 服务 身份证和姓名 钱包 身份证服务\身份证余额 $statistic = Wallets::leftJoin('service', 'service.id', '=', 'wallets.service_id')->select('name as label', 'balance as value')->where('balance', '>', 0)->whereYear('wallets.updated_at', $now->year)-

有带
钱包的桌子
服务

服务

身份证和姓名

钱包

身份证服务\身份证余额

$statistic = Wallets::leftJoin('service', 'service.id', '=', 'wallets.service_id')->select('name as label', 'balance as value')->where('balance', '>', 0)->whereYear('wallets.updated_at', $now->year)->whereMonth('wallets.updated_at', $now->month)->get();
并得到结果

[{"label":"Service1","value":0.0711679},
 {"label":"Service1","value":0.015},
 {"label":"Service2","value":0.065572},
 {"label":"Service2","value":0.02},
 {"label":"Service3","value":0.0206064},
 {"label":"Service2","value":0.04399}]
但是在
->groupBy('label')之后:

仅获取第一个结果


<?php 

// If you are referring count as total balance then you can do this
$statistic = Wallets::leftJoin('service', 'service.id', '=', 'wallets.service_id')
->where('balance', '>', 0)
->whereYear('wallets.updated_at', $now->year)
->whereMonth('wallets.updated_at', $now->month)
->select(
    \DB::raw('name as label'), 
    \DB::raw('SUM(balance) as value')
)
->groupBy('name')
->get();

// If you are referring count as total entries for each label then you can do this
$statistic = Wallets::leftJoin('service', 'service.id', '=', 'wallets.service_id')
->where('balance', '>', 0)
->whereYear('wallets.updated_at', $now->year)
->whereMonth('wallets.updated_at', $now->month)
->select(
    \DB::raw('name as label'), 
    \DB::raw('balance as value'),
    \DB::raw('count(*) as aggregate')
)
->groupBy('name')
->get();
<?php 

// If you are referring count as total balance then you can do this
$statistic = Wallets::leftJoin('service', 'service.id', '=', 'wallets.service_id')
->where('balance', '>', 0)
->whereYear('wallets.updated_at', $now->year)
->whereMonth('wallets.updated_at', $now->month)
->select(
    \DB::raw('name as label'), 
    \DB::raw('SUM(balance) as value')
)
->groupBy('name')
->get();

// If you are referring count as total entries for each label then you can do this
$statistic = Wallets::leftJoin('service', 'service.id', '=', 'wallets.service_id')
->where('balance', '>', 0)
->whereYear('wallets.updated_at', $now->year)
->whereMonth('wallets.updated_at', $now->month)
->select(
    \DB::raw('name as label'), 
    \DB::raw('balance as value'),
    \DB::raw('count(*) as aggregate')
)
->groupBy('name')
->get();