I have 3 Models... Category, Post, Vote
When viewing a category, I am showing an index of all Posts in that category. So I'm doing something like foreach ($category->posts as $post)
in my view.
The question I have now is how can I order the posts based on the sum of votes they have?
I have standard relationships setup, so that a post hasMany votes.
You can do it either by defining a helper relation on the
Post
model and sorting the collection after the relation is loaded OR simply by joining the votes and ordering in the query.1 RELATION
2 JOIN
You can use scope for that: