Laravel OrderBy relationship count

后端 未结 5 936
Happy的楠姐
Happy的楠姐 2020-11-28 06:24

I\'m trying to get the most popular hackathons which requires ordering by the respective hackathon\'s partipants->count(). Sorry if that\'s a little difficul

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 06:52

    This works for me in Laravel 5.3, using your example:

    Hackathon::withCount('participants')->orderBy('participants_count', 'desc')->paginate(10); 
    

    This way it is ordered on the query and the pagination works nicely.

提交回复
热议问题