l2t

Laravel orderByRaw function

Asked: 19 days ago

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

# Using Model
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();
   
Viewed: 49 times
Liked: 2 times