回答編集履歴
2
コード修正
answer
CHANGED
@@ -18,13 +18,13 @@
|
|
18
18
|
->whereDate('date', '<=', '2018-08-31')
|
19
19
|
->get()
|
20
20
|
->groupBy('date')
|
21
|
-
->map(function (
|
21
|
+
->map(function ($byDate) {
|
22
22
|
return $byDate
|
23
23
|
->groupBy('product_id')
|
24
|
-
->map(function (
|
24
|
+
->map(function ($byProductId) {
|
25
25
|
return $byProductId
|
26
26
|
->groupBy('type')
|
27
|
-
->map(function (
|
27
|
+
->map(function ($byType) {
|
28
28
|
return $byType->first()['count'];
|
29
29
|
})
|
30
30
|
->all();
|
1
groupByとmapを使った例を追記
answer
CHANGED
@@ -9,4 +9,27 @@
|
|
9
9
|
->each(function ($item) use (&$dictionary) {
|
10
10
|
$dictionary[$item['date']][$item['product_id']][$item['type']] = $item['count'];
|
11
11
|
});
|
12
|
+
```
|
13
|
+
|
14
|
+
最初に書いたやり方だとこんな感じ。
|
15
|
+
|
16
|
+
```php
|
17
|
+
$dictionary = App\HogeTable::whereDate('date', '>=', '2018-08-01')
|
18
|
+
->whereDate('date', '<=', '2018-08-31')
|
19
|
+
->get()
|
20
|
+
->groupBy('date')
|
21
|
+
->map(function (Collection $byDate) {
|
22
|
+
return $byDate
|
23
|
+
->groupBy('product_id')
|
24
|
+
->map(function (Collection $byProductId) {
|
25
|
+
return $byProductId
|
26
|
+
->groupBy('type')
|
27
|
+
->map(function (Collection $byType) {
|
28
|
+
return $byType->first()['count'];
|
29
|
+
})
|
30
|
+
->all();
|
31
|
+
})
|
32
|
+
->all();
|
33
|
+
})
|
34
|
+
->all();
|
12
35
|
```
|