2

Using Laravel eloquent how would on retrieve a count of store sales by date for example I would want

Result

+---------+--------+--------+-------+
| Store   | 17/01  | 18/01  | 19/01 |
+---------+--------+--------+-------+
| 1       | 0      | 1      | 1     |
| 2       | 1      | 1      | 0     |
+---------+--------+--------+-------+

Table

+-------+--------+-------+
| Sale  | Store  | Date  |
+-------+--------+-------+
| 1     |  1     | 19/01 |
| 1     |  1     | 18/01 |
| 1     |  2     | 18/01 |
| 1     |  2     | 17/01 |
+-------+--------+-------+

I have tried this

 $contracts = \App\Sales::select('Store','Date')
            ->where('Date', '>=','1/1/2016')
            ->groupBy('Store','Date')
            ->get();

This returns a collection of data, I know I could loop through the data in a foreach but was wondering could I do it in sql easier?

yAnTar
  • 4,269
  • 9
  • 47
  • 73
Darkskies16
  • 71
  • 1
  • 8

0 Answers0