Laravel OrderBy relationship count

后端 未结 5 934
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 07:18

    Another approach can be by using withCount() method.

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

    Ref: https://laravel.com/docs/5.5/eloquent-relationships#querying-relations

提交回复
热议问题