I have a statistics page. I need to plot the graph with sum (amount) and sum (total_amount) of different booking date. I have tried some code but getting Collection {#277 #items: [] }.
Please check my code and correct me Thanks. I have given below expecting result and booking table structure.
My db is mongodb. Project is developing using laravel
DataType
amount (string) total_amount (string) bookingdate (date)
booking (table)
_id amount total_amount bookingdate
57ea5a3bf8f888c40900002a 25 50 2016-09-30 17:06:18.000
57ea5a3bf8f888c40888883a 20 55 2016-09-30 11:07:19.000
57ea5a3bf8f888c40999998a 15 30 2017-02-15 17:06:18.000
57ea5a3bf8f888c40444445a 35 55 2017-02-15 09:06:18.000
57ea5a3bf8f888c40966667a 39 67 2017-05-11 10:06:18.000
57ea5a3bf8f888c40967806a 43 49 2017-05-11 11:06:18.000
Expecting result
amount total_amount bookingdate (date)
45 105 2016-09-30
50 85 2017-02-15
82 116 2017-05-11
Code
if(!empty($request->cabin) && !empty($request->daterange))
{
$cabinName = $request->cabin;
$daterange = explode("-", $request->daterange);
$dateBegin = new \MongoDB\BSON\UTCDateTime(strtotime($daterange[0])*1000);
$dateEnd = new \MongoDB\BSON\UTCDateTime(strtotime($daterange[1])*1000);
$bookings = Booking::raw(function ($collection) {
return $collection->aggregate([
[
'$match' => [
'is_delete' => 0,
'cabinname' => '$cabinName',
'checkin_from' => ['$gte' => '$dateBegin', '$lte' => '$dateEnd']
],
],
[
'$group' => [
'_id' => [
'month' => ['$month' => '$checkin_from'],
'day' => ['$dayOfMonth' => '$checkin_from'],
'year' => ['$year' => '$checkin_from']
],
'prepayment_amount' => [
'$sum' => '$prepayment_amount',
],
'total_prepayment_amount' => [
'$sum' => '$total_prepayment_amount',
]
]
]
]);
});
dd($bookings);
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire