title: Combine whereRaw() with leftjoin() using eloquent
body: <p>I am trying to join 2 tables and get the latest unique results using <code>whereRaw()</code> and <code>leftJoin()</code> with Laravel eloquent.</p>
  <p>I have 2 tables:-</p>
  <p>skills table (has timestamps):-</p>
  <pre><code>| id|   name  |  icon      |
  | 1 | skill 1 | skill1.png |
  | 2 | skill 2 | skill2.png |
  | 3 | skill 3 | skill3.png |
  </code></pre>
  <p>scores table (has timestamps):-</p>
  <pre><code>| id| player_id | skill_id | score |
  | 1 | 1         | 1        | 1     |
  | 2 | 1         | 2        | 1     |
  | 3 | 1         | 3        | 1     |
  | 4 | 1         | 2        | 2     |
  </code></pre>
  <p>I would like to return all skills but only the latest entry(by id), so for the above snippet I should get:-</p>
  <pre><code>| id| player_id | name    | skill_id | score |
  | 1 | 1         | skill 1 | 1        | 1     |
  | 3 | 1         | skill 3 | 1        | 1     |
  | 4 | 1         | skill 2 | 2        | 2     |
  </code></pre>
  <p>I can get the latest unique records by using:</p>
  <pre><code>return SkillScores::where('player_id', $this-&gt;id)
          -&gt;whereRaw('id in (select max(id) from skills group by (name))')
          -&gt;get();
  </code></pre>
  <p>I can get the skill names by using:-</p>
  <pre><code>return SkillScores::where('player_id', $this-&gt;id)
          -&gt;leftJoin('skills', 'skill_scores.skill_id', '=', 'skills.id')
          -&gt;get();
  </code></pre>
  <p>but when I combine them I get an SQLSTATE[23000] error</p>
  <pre><code>return SkillScores::where('player_id', $this-&gt;id)
          -&gt;whereRaw('id in (select max(id) from skills group by (name))')
          -&gt;leftJoin('skills', 'skill_scores.skill_id', '=', 'skills.id')
          -&gt;get();
  </code></pre>
  <p>Can anyone help me figure out what is going wrong?</p>
  <p>EDIT:-</p>
  <p>It turns out that the SQLSTATE[23000] error is occuring because I have an id column in both tables and I havent told it which one I am referencing, the below fixes the issue and gives me the correct result.</p>
  <pre><code>return SkillScores::where('player_id', $this-&gt;id)
          -&gt;whereRaw('skill_scores.id in (select max(skill_scores.id) from skill_scores group by (skill_id))')
          -&gt;leftJoin('skills', 'skill_scores.skill_id', '=', 'skills.id')
          -&gt;get();
  </code></pre>