title: How to return highest id value from a table via join with eloquent Laravel 5.2
body: <p>i have two tables that share a relationship users and log table. I’m trying to query the users table and get the record from log with the highest id value</p>
  
  <p>so far this is what I have that’s returning duplicate entries:</p>
  
  <pre><code>  $students = User::with([
          'course' =&gt; function ($query) {
              $query-&gt;get(['id', 'name']);
          }
      ])
          -&gt;join('log', 'users.id', '=', 'log.user_id')
          -&gt;where('log.event', 1)
          -&gt;orderBy('log.id', 'desc')
          -&gt;where('users.verified', 1)
          -&gt;get(['users.*', 'log.id AS logid']);
  </code></pre>
  
  <p>Ideally, I want the last inserted record from the log table for each user</p>
  
  <pre><code>groupBy('user_id')
  </code></pre>
  
  <p>returns the first record</p>