I have a requirement to get the data grouped by created_date
, and then again to grop this data from the result set on affiliate_ad
. I use this
return DB::table($this->table)
->whereRaw($where['rawQuery'], isset($where['bindParams']) ? $where['bindParams'] : array())
->select('id', 'created_date','affiliate_ad', DB::raw('count(*) as total,count(affiliate_ad=1) as affiliate_ad_count,SUBSTRING(`created_date`, 1, 10) AS c_date'))
->groupBy('affiliate_ad','c_date')
->orderBy('c_date', 'desc')
->get();
It gives me a result like this
Collection {
0 => {
+"id": 354766
+"created_date": "2018-01-10 10:16:27"
+"affiliate_ad": 1
+"total": 2
+"affiliate_ad_count": 1
+"c_date": "2018-01-10"
}
1 => {
+"id": 354730
+"created_date": "2018-01-10 10:10:39"
+"affiliate_ad": 0
+"total": 3
+"affiliate_ad_count": 4
+"c_date": "2018-01-10"
}
2 => {
+"id": 338263
+"created_date": "2018-01-08 10:10:52"
+"affiliate_ad": 0
+"total": 83
+"affiliate_ad_count": 83
+"c_date": "2018-01-08"
}
]
}
Here, if you check, in the first two indexes the created date will be the same. So I want to group them into one index of an array with an index 0th
as multidimensional array
grouped by affiliate_ad
. The actual request is created as
SELECT id
, created_date
, affiliate_ad
, COUNT(*) total
, COUNT(affiliate_ad = 1) affiliate_ad_count
, SUBSTRING(created_date,1,10) c_date
FROM facebook_ad
WHERE facebook_id = 12345
AND reward_status = 0
AND (first_seen BETWEEN 0 AND 99999999)
GROUP
BY affiliate_ad
, c_date
ORDER
BY c_date desc
I need a conclusion like this
Collection {
0 => [
0 => {
+"id": 354766
+"created_date": "2018-01-10 10:16:27"
+"affiliate_ad": 1
+"total": 2
+"affiliate_ad_count": 1
+"c_date": "2018-01-10"
}
1 => {
+"id": 354730
+"created_date": "2018-01-10 10:10:39"
+"affiliate_ad": 0
+"total": 3
+"affiliate_ad_count": 4
+"c_date": "2018-01-10"
}
]
1 => [
0 => {
+"id": 338263
+"created_date": "2018-01-08 10:10:52"
+"affiliate_ad": 0
+"total": 83
+"affiliate_ad_count": 83
+"c_date": "2018-01-08"
}
]
]
}
I have this data in mysql