2 years ago

Order by relationship count

Posted 2 years ago by lucavallin

Hi, I see there are other questions like this but I didn't like the answers. I am building an incredibly simple vote-idea-for-the-company app which has a Topic (title, description) that has many users (voters). I'd like to display the topics sorted by votes count and date, which I am doing like this:

        $topics = Topic::select(
                DB::raw('(SELECT count(*) FROM topic_user WHERE topic_id = id) as count_voters'))

This however doesn't sort by date, and it's really ugly to see. Suggestions?

Please sign in or create an account to participate in this conversation.