Laravel orderByRaw function

Defination

Laravel orderByRaw allows you to write raw sql for your order by clause. By default laravel does not allow you to write raw queries insider orderBy function.

Examples

Post::select("*")
  ->where("published", 1)
  ->orderByRaw("(views + likes) > 1000")
  ->get();

# Using Query Builder
DB::table('posts')
  ->select("*")
  ->where("published", 1)
  ->orderByRaw("(views + likes) > 1000")
  ->get();
​