ORDER BY in string date

1

I have a table in the database, novidades , where I have a column named date is to be stored as string in dd-mm-yyyy format, but I would like to sort by date based on this value , I calculate that you have to convert to date/datetime at select time.

What I'm trying to do now is nothing more than this:

Novidade::orderBy('date', 'DESC')->get();

However it does not work, the sorting goes wrong understandably.

NOTE : I can not move the table and change the column type.

    
asked by anonymous 27.11.2017 / 14:32

1 answer

1

Use orderByRaw with your bank's role, for example, to use str_to_date to write the text conversion function to date, example / strong>:

Novidade::orderByRaw("STR_TO_DATE(date,'%d/%m/%Y') DESC")->get();

Note: The good thing would be if the data had the field in the correct format, that is, converted to date/datetime will have higher cost in executing your SQL .

References:

27.11.2017 / 14:38