teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

コード修正

2018/09/06 01:33

投稿

crhg
crhg

スコア1177

answer CHANGED
@@ -18,13 +18,13 @@
18
18
  ->whereDate('date', '<=', '2018-08-31')
19
19
  ->get()
20
20
  ->groupBy('date')
21
- ->map(function (Collection $byDate) {
21
+ ->map(function ($byDate) {
22
22
  return $byDate
23
23
  ->groupBy('product_id')
24
- ->map(function (Collection $byProductId) {
24
+ ->map(function ($byProductId) {
25
25
  return $byProductId
26
26
  ->groupBy('type')
27
- ->map(function (Collection $byType) {
27
+ ->map(function ($byType) {
28
28
  return $byType->first()['count'];
29
29
  })
30
30
  ->all();

1

groupByとmapを使った例を追記

2018/09/06 01:33

投稿

crhg
crhg

スコア1177

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
  ```