Join two tables and add the data with the same column id <p>I am using Laravel 4.2<br> I have two tables "questions" and "votes"<br> In my questions table I have columns like</p> <ul> <li><code>id</code>,</li> <li><code>id_user</code>,</li> <li><code>subject</code>,</li> <li><code>body</code>.</li> </ul> <p>In my votes table I have columns like </p> <ul> <li><code>id</code>,</li> <li><code>id_question</code>,</li> <li><code>id_user</code>, </li> <li><code>likes</code>,</li> <li><code>dislikes</code>.</li> </ul> <p>The user is allowed to vote only once therefore the values for likes and dislikes for each user is 0-1, the <code>id_question</code> has the <code>id</code> of the question from the questions table. Now U want to sort the questions in the descending order according to the number of votes cast for each question.</p> <p>How do I sum the votes and sort them. This is what I have tried but it just displays me the count of the first question</p> <pre><code>$questions=Question::leftJoin('votes', 'questions.id', '=', 'votes.id_question') -&gt;groupBy('questions.id')-&gt;sum('likes'); return json_encode($questions); </code></pre> <p>after I get the counts i can sort them in the descending order by using <code>orderBy('likes','DESC')-&gt;get();</code> but how to get the sum of likes?</p>