php - How do I sum a group collection in Laravel?

one text

I have a grouped collection of Items and Prices from my database in Laravel, I want to sum the total price in my blade template after each group. thus, to display the total sum after each group. Am unable to sum after the grouping. is there a different way to do that? please assist, thank you

my Code below: my Controller

 public function departmentSummary($id)
    {
        $items = \DB::table('invoices')
        ->where('user_id', $id)
        ->where('invoice_status', 1)
        ->whereDate('created_at', Carbon::today())
        ->get();

        $grouped = $items->groupBy('invoice_category');
        return view('inc.department-summary', [
            'group' => $grouped->all()
        ]);
    }

my view Template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Department Report</title>
</head>
<body>
    <style>
        body{
            font-size:15px;
            margin: auto;
            padding: 15px;

        }


        table, th, td {
            border: 1px solid black;
            }
    </style>


<?php

$department = $group;

foreach ($department as $category => $group) {
 echo "<h2>$category</h2>";
 echo "<table>";
   foreach ($group as $group) {
    echo "<tr>";
        echo "<td>";
            echo "Name of Item";
        echo "</td>";
        echo "<td>";
            echo "Qty";
        echo "</td>";
        echo "<td>";
            echo "Amount";
        echo "</td>";
    echo "</tr>";
     echo "<td>".$group->invoice_product_name ."</td>";
     echo "<td>".$group->invoice_quantity."</td>";
     echo "<td>".$group->invoice_total_price."</td>";
   }
 echo "</table>";

echo "Total:" . $group->sum('invoice_total_price') ;
}



?>
</body>
</html>

echo "Total:" . $group->sum('invoice_total_price') ;

is not able to sum, How Can I properly do this?

Source