Reputation: 466
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
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.
DB::raw
to be able to use SQL functionsDB::raw
expects a stringUpvotes: 4