Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
Php 按类别计算Laravel中的总金额_Php_Laravel_Eloquent - Fatal编程技术网

Php 按类别计算Laravel中的总金额

Php 按类别计算Laravel中的总金额,php,laravel,eloquent,Php,Laravel,Eloquent,我提出这个问题: $expenses = Expense::where('team_id', Auth::user()->currentTeam->id) ->with(['categories']) ->orderBy('entry_date', 'DESC') ->get(); 费用消耗类别资源: return [ 'id' => Hashid

我提出这个问题:

$expenses = Expense::where('team_id', Auth::user()->currentTeam->id)
            ->with(['categories'])
            ->orderBy('entry_date', 'DESC')
            ->get();
费用消耗类别资源:

return [
            'id'         => Hashids::encode($this->id),
            'entry_date' => $this->entry_date,
            'amount'     => $this->amount,
            'category' => $this->category->name
 ];
答复:

"expenses": [
        {
            "id": "GO3W1MkgWkngpnByQ6",
            "entry_date": "2020-10-14",
            "amount": "50.00",
            "category": "Health"
        },
        {
            "id": "7vK6bz0gnG7JBOYRLD",
            "entry_date": "2020-10-14",
            "amount": "30.00",
            "category": "Uncategorize"
        },
        {
            "id": "7vK6bz0gnG7JBOYRLD",
            "entry_date": "2020-10-14",
            "amount": "30.00",
            "category": "Health"
        },
        {
            "id": "7vK6bz0gnG7JBOYRLD",
            "entry_date": "2020-10-14",
            "amount": "30.00",
            "category": "Food"
        },
    ]
我想按类别计算“金额”的总和

理想情况下,我希望得到如下结果:

"expenses": [
        {
            "category": "Health"
            "total_amount": "80.00",
        },
        {
            "category": "Uncategorized"
            "total_amount": "30.00",
        },
        {
            "category": "Food"
            "total_amount": "30.00",
        },
    ]

您可以尝试添加对您的查询有意义的
select()
groupBy()

$expenses = Expense::select('category', DB::raw('SUM(amount) AS total_amount'))
    ->where('team_id', Auth::user()->currentTeam->id)
    ->groupBy('category')
    ->with(['categories'])
    ->orderBy('category')
    ->get();

你好它不起作用,因为如我的示例所示,我正在使用一个资源文件来调用“category”,我无法帮助您。看看你能不能让我的答案起作用。