我有以下数据库:
Reservations
| id | status | created_at |
| 1 | Canceled | 2019-12-16 11:46:11 |
| 2 | Fulfilled | 2019-11-15 23:03:24 |
| 3 | Accepted | 2019-12-13 12:04:13 |
所以我想按月份提取一个集合,其中包含每个状态的计数。 即:
['Month', 'Accepted', 'Fulfilled', 'Canceled'],
['Jul 2019', 4, 5, 9],
['Aug 2019', 5, 6, 7]
我有几种方法可以实现这一点,但都需要在 eloquent 之外进行额外的工作,而且看起来不太优雅。
最佳答案
尝试使用 Collection::countBy() 方法。 (https://laravel.com/docs/master/collections#method-countBy)
Reservation::countBy('status');
在调用 countBy()
函数之前,您始终可以限制查询
关于laravel - 按月提取每个状态的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59426822/