Sunday, April 16, 2017

(laravel) Two ORDER BY clauses and a LIMIT

This is some sorting:

  1. I want to list today's 20 best posts based on "views".
  2. then, sort it based on "created_at".

How to do it?

Heres my current code (which doesnt work):

$post = 
Posts::orderBy('views', 'desc')
->orderBy('created_at', 'desc')
->whereRaw('created_at >= NOW() - INTERVAL 1 DAY')
->limit(20)->get();



via dobieme

Advertisement