0

We are retrieving the list from the database which is having date. We want to display it in different format. Suppose 10 Nov, 2014.

Here is my Code

Trail1:

$dropDown = Events::lists(DATE_FORMAT(event_date,'%d %M, %Y'), 'id');

Trail 2:

$dropDown = Events::lists("DATE_FORMAT(event_date, '%Y-%m-%d')  AS event_date", 'id')

But it throws error. Is there any way that we can use, to get desired date format in Laravel?

Normal Query:

select id, DATE_FORMAT(event_date,'%d %M, %Y') AS event_date from events
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
vvr
  • 456
  • 3
  • 17
  • 30

1 Answers1

4

This should work.

Events::lists(DB::raw('DATE_FORMAT(event_date, "%d %M, %Y")'), 'id');

In the code you posted in the question (and the comments) there's always a small mistake.

  1. DATE_FORMAT is an SQL function not a PHP function
  2. You have to use DB::raw to be able to use SQL functions
  3. DB::raw expects a string
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • the query is printing like this: select DATE_FORMAT(event_date, "%d %M, %Y"), `id` from `events` – vvr Nov 10 '14 at 14:06
  • is there any way that i can add as event_date or something like that – vvr Nov 10 '14 at 14:06
  • @vvr When using `lists` that wouldn't make much sense, because the return value is an array with id and value `[1 => "10 November, 2014"` – lukasgeiter Nov 10 '14 at 14:26