I have these timestamp columns in database table - expiration, edit, created_at. I need to order by 'edit', if item 'expiration' date is bigger than today's date, else I need to order by 'created_at'.
I am trying something like this, but it isn't working correctly
$items = $items
->orderBy(DB::raw("CASE WHEN expiration >= $time THEN edit ELSE created_at END"), 'DESC')
->get();
or
$items = $items
->orderBy(DB::raw("CASE WHEN expiration >= $time THEN edit END"), 'DESC')
->orderBy('created_at', 'DESC')
->get();
Variable $time is correct, so my problem is in query. Sample data:
id name created_at expiration edit
1. it1 2015-03-16 15:42:40 0000-00-00 00:00:00 2015-03-16 15:42:40
2. it2 2015-03-16 15:37:27 2015-03-16 00:00:00 2015-03-16 15:37:27
3. it3 2015-03-16 12:36:50 2015-03-27 00:00:00 2015-03-16 14:52:19
And i need in order -> it3, it1, it2
Variable $time = 2015-03-17
X
orY
, then this is what you need. Maybe you want to order byX
first, then byY
? Show sample data and expected output. – Bicipital