title: How to write mongo group by and sum aggregate functions in laravel eloquent raw method?
body: <p>I wrote some code to fetch data within date range using laravel eloquent raw method. But I don't know how to write mongodb <code>groupby</code> and <code>sum</code> in laravel eloquent raw method. Please help me to take <code>sum(prepayment_amount)</code> and <code>sum(total_prepayment_amount)</code> group by <code>checkin_from</code> date. The expecting result is given below. Thanks</p>
  
  <p><strong>DashbardController.php</strong></p>
  
  <pre><code>    $cabinName              = $request-&gt;cabin;
      $daterange              = explode("-", $request-&gt;daterange);
      $dateBegin              = new \MongoDB\BSON\UTCDateTime(strtotime($daterange[0])*1000);
      $dateEnd                = new \MongoDB\BSON\UTCDateTime(strtotime($daterange[1])*1000);
      $bookings               = Booking::raw(function ($collection) use ($cabinName, $dateBegin, $dateEnd) {
         return $collection-&gt;aggregate([
            [
              '$match' =&gt; [
                           'is_delete' =&gt; 0,
                           'cabinname' =&gt; $cabinName,
                           'checkin_from' =&gt; ['$gte' =&gt; $dateBegin, '$lte' =&gt; $dateEnd]
                           ],
            ],
         ]);
     });
     dd($bookings);
  </code></pre>
  
  <p><strong>Booking Collection</strong></p>
  
  <pre><code>    { "_id" : ObjectId("58046a49f8f888a80b00002a"), "cabinname" : "Matras-Haus", "checkin_from" : ISODate("2016-09-17T00:00:00Z"), "prepayment_amount" : "20", "total_prepayment_amount" : "25", "bookingdate" : ISODate("2016-09-17T06:06:01Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1), "payment_status" : "1" }
      { "_id" : ObjectId("58183678d2ae67a404431d5c"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2016-10-31T23:00:00Z"), "prepayment_amount" : "30", "total_prepayment_amount" : "40", "bookingdate" : ISODate("2016-11-01T06:30:16Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1), "payment_status" : "0" }
      { "_id" : ObjectId("581b31f3d2ae674d5f431d5b"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2016-10-31T23:00:00Z"), "prepayment_amount" : "35", "total_prepayment_amount" : "45", "bookingdate" : ISODate("2016-11-03T12:47:47Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
      { "_id" : ObjectId("5821af65d2ae67c82154efc5"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2016-09-05T22:00:00Z"), "prepayment_amount" : "40", "total_prepayment_amount" : "45", "bookingdate" : ISODate("2016-11-08T10:56:37Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
      { "_id" : ObjectId("582558d4d2ae679c4d8b4567"), "cabinname" : "Matras-Haus", "checkin_from" : ISODate("2016-09-17T22:00:00Z"), "prepayment_amount" : "20", "total_prepayment_amount" : "23", "bookingdate" : ISODate("2016-08-17T05:36:20Z"), "is_delete" : NumberLong(1) }
      { "_id" : ObjectId("58352c3cd2ae672341ec89e1"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2016-09-05T22:00:00Z"), "prepayment_amount" : "50", "total_prepayment_amount" : "55", "bookingdate" : ISODate("2016-08-05T05:42:20Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
      { "_id" : ObjectId("58352c3cd2ae672341ec887e1"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-10-05T22:00:00Z"), "prepayment_amount" : "50", "total_prepayment_amount" : "55", "bookingdate" : ISODate("2017-08-05T05:42:20Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
      { "_id" : ObjectId("58352c3cd2ae672341ec887e1"), "cabinname" : "Matras-Haus", "checkin_from" : ISODate("2017-10-05T22:00:00Z"), "prepayment_amount" : "50", "total_prepayment_amount" : "55", "bookingdate" : ISODate("2017-08-05T05:42:20Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
  </code></pre>
  
  <p><strong>Expecting result</strong></p>
  
  <pre><code>prepayment_amount total_prepayment_amount    checkin_from  cabinname
  
  40                       48                  2016-09-17    Matras-Haus
  
  50                       55                  2017-10-05    Matras-Haus
  
  65                       85                  2016-10-31    Kemptner Hütte
  
  90                       110                 2016-09-05    Kemptner Hütte
  
  50                       55                  2017-10-05    Kemptner Hütte
  </code></pre>