Ordering records using dates in Laravel eloquent

radioactive :

I have a column move_out that stores date in this format - m/Y e.g 02/2020. I want to order the records in that table by the move_out column in descending order but my solution don't seem to be working. It does not arrange the records appropriately. This is what I am doing.

$data = User::orderBy('move_out', 'DESC')->get();

How do I solve this?

The datatype for the move_out column is string.

Ronak Dhoot :

try STR_TO_DATE

$data = User::orderBy(DB::raw("STR_TO_DATE(CONCAT('01-', move_out),'%d-%m/%Y')"), 'DESC')->get();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169887&siteId=1