How do I return a collection of dates part of timestamp


I have checked around but can't seem to find a solution to this, which is why I'm asking for help here. I am working on a project with Laravel 5.8. I have a mysql table storing customer's orders. The orders table have among other columns ID, order No and a created_at column storing the timestamp of the order. Now I want to return a collection that contains all the dates there are orders and possibly the number of orders for those dates, returned just once like so; 7/10/2019 - 5 6/10/2019 - 6 5/10/2019 - 12 4/10/2019 - 9 I want only the date part of the timestamp of the days when there are orders. The days without orders should be skipped. I have done this;

Order::selectRaw('Date(created_at) as date')->groupBy('date')->get();
But keep getting only today's date.
  • Have you tried running the sql query directly against the database? Are you sure it works doing groupBy('date') using the alias, maybe you need groupBy('Date(created_at)')? – Joakim Danielson 1 hour ago 
  • Yes @Joachim. It however returns a collection of the orders with today's date as date for all of them – banky 1 hour ago
  • How can it return a collection of orders if you are using GROUP BY? – Joakim Danielson 1 hour ago

Your Answer

Post a Comment

0 Comments