【发布时间】:2021-05-09 00:35:50
【问题描述】:
我有以下
->select(DB::raw('source as Source, customer as Customers, COUNT(*) as count'))
->groupBy('source', 'customer')
->get();
我得到以下结果
Illuminate\Support\Collection {#460 ▼
#items: array:4 [▼
0 => {#466 ▼
+"Source": "Facebook"
+"Customer": "Yes"
+"count": 227
}
1 => {#463 ▼
+"Source": "PinInterest"
+"Customer": "Yes"
+"count": 370
}
2 => {#465 ▼
+"Source": "PinInterest"
+"Customer": "No"
+"count": 133
}
3 => {#467 ▼
+"Source": "Whatsapp"
+"Customer": "No"
+"count": 254
}
]
}
现在 Source 可以是 1 到 10 个不同的渠道,具体取决于客户。 现在客户是或否。
如何添加修改集合以添加 Source Facebook、Customer No 和 Count 0,以及 Source WhatsApp、Customer Yes 和 Count 0
来源:脸书 客户:是的 计数:227
来源:脸书 客户:没有 计数:0
来源:Whatsapp 客户:没有 计数:254
来源:Whatsapp 客户:是的 计数:0
【问题讨论】:
-
这是什么意思
Finally I want the collection to be like [ Facebook "No" doesn't Exits and Whatsapp "Yes" doesn't Exists] -
对不起,我更正了。表中没有 Facebook 的任何行。 Table 也有 WhatsApp Customer 的一行,但名称不存在,所以计数为 0。
-
客户是否只有两个选项是和否?或者可以有很多
-
@MKhalidJunaid,只有是和否。
-
如果你只是想为你知道值的属性做这件事,那么只使用集合中的transform()怎么样,
标签: laravel eloquent collection-select