Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cooperino's avatar

SELECT by MONTH and FORMAT with DB::raw

I want to fetch data by month, so I have the following query:

->select(DB::raw('MONTH(created_at) as date'))

But then it returns the date as the number of the month. So I tried to add FORMAT to the query, but it's not working:

->select(DB::raw('FORMAT (MONTH(created_at), "MM-yy") as date'))

how can I use the FORMAT correctly?

0 likes
4 replies
cooperino's avatar

@richardhulbert The problem I need to both use MONTH because later I want to group by the month so I need to do FORMAT on MONTH

*Edit: Thank you, my bad. Now it works. Btw, I need to format it on the query itself since I send this data as is back to the frontend as it expects this format so it's quicker to execute

cooperino's avatar

@sr57 You're right, my bad I was supposed to completely replace MONTH. It works,

Please or to participate in this conversation.