vvr
vvr

Reputation: 466

Laravel how to change date format in the list

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

Upvotes: 0

Views: 5426

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 153150

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

Upvotes: 4

Related Questions