Home > other >  how to order by sum of counts of many columns?
how to order by sum of counts of many columns?

Time:11-27

i have posts table and related with likes table ,comments table and shares table i use

 `Post::withCount("likes")->orderByDesc("likes_count")->paginate($per_page);`

to get posts order by likes count .

**i need to get posts order by maxcount of(likes   comments  shares)** ?

ِAny Suggesstion ?

CodePudding user response:

You can use orderByRaw method

Post::withCount(['likes', 'comments', 'shares'])
    ->orderByRaw('likes_count   comments_count   shares_count DESC')
    ->paginate($per_page);
  • Related