'laravel eloquent SUM with relation

Hi guys I want to calulate sum of rate for comments group by type like this

Post::with(['comments' => function ($q) { 
      $q->selectRaw('type, SUM(rate) as total_rate') 
         ->groupBy('type'); 
}])

I'm waiting for a result like this:

    0 => array:4 [ 
        "id" => 5 
        "start_date" => "2022-01-01" 
        "end_date" => "2022-01-31" 
        "comments" => array:2 [
           0 => array:3 [
             "type" => "personal"
             "total_rate" => 44244.0
          ]
          1 => array:3 [
            "type" => "business"
            "total_rate" => 22358.0
         ]
       ]

but the result is

0 => array:4 [ 
"id" => 5 
"start_date" => "2022-01-01" 
"end_date" => "2022-01-31" 
"comments" => [] 
]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source