title: Laravel eager loading with limit
body: <p>I have two tables, say "users" and "users_actions", where "users_actions" has an hasMany relation with users:</p>
  
  <p><strong>users</strong></p>
  
  <pre><code>id | name | surname | email...
  </code></pre>
  
  <p><strong>actions</strong></p>
  
  <pre><code>id | id_action | id_user | log | created_at
  </code></pre>
  
  <p>Model Users.php</p>
  
  <pre><code>class Users {
      public function action()
      { 
         return $this-&gt;hasMany('Action', 'user_id')-&gt;orderBy('created_at', 'desc');
      }
  }
  </code></pre>
  
  <p>Now, I want to retrieve a <strong>list</strong> of all <strong>users</strong> with <strong>their LAST action</strong>. </p>
  
  <p>I saw that doing  <code>Users::with('action')-&gt;get();</code>
  can easily give me the last action by simply fetching only the first result of the relation:</p>
  
  <pre><code>foreach ($users as $user) {
     echo $user-&gt;action[0]-&gt;description;
  }
  </code></pre>
  
  <p>but I wanted to avoid this of course, and just pick ONLY THE LAST action for EACH user.</p>
  
  <p>I tried using a constraint, like</p>
  
  <pre><code>Users::with(['action' =&gt; function ($query) {
      $query-&gt;orderBy('created_at', 'desc')
            -&gt;limit(1);
      }])
  -&gt;get();
  </code></pre>
  
  <p>but that gives me an incorrect result since Laravel executes this query:</p>
  
  <pre><code>SELECT * FROM users_actions WHERE user_id IN (1,2,3,4,5)
  ORDER BY created_at
  LIMIT 1
  </code></pre>
  
  <p>which is of course wrong. Is there any possibility to get this without executing a query for each record using Eloquent?
  Am I making some obvious mistake I'm not seeing? I'm quite new to using Eloquent and sometimes relationship troubles me.</p>
  
  <p><strong>Edit:</strong> </p>
  
  <p>A part from the representational purpose, I also need this feature for <strong>searching inside a relation</strong>, say for example I want to search users where LAST ACTION = 'something'</p>
  
  <p>I tried using </p>
  
  <pre><code>$actions-&gt;whereHas('action', function($query) {
      $query-&gt;where('id_action', 1);
  });
  </code></pre>
  
  <p>but this gives me ALL the users which had had an action = 1, and since it's a log everyone passed that step.
  <br />
  <br />
  <br /></p>
  
  <h2>Edit 2:</h2>
  
  <p>Thanks to @berkayk looks like I solved the first part of my problem, but still I can't search within the relation. </p>
  
  <pre><code>Actions::whereHas('latestAction', function($query) {
      $query-&gt;where('id_action', 1);
  });
  </code></pre>
  
  <p>still doesn't perform the right query, it generates something like:</p>
  
  <pre><code>select * from `users` where 
   (select count(*) 
     from `users_action` 
     where `users_action`.`user_id` = `users`.`id` 
     and `id_action` in ('1')
    ) &gt;= 1 
  order by `created_at` desc
  </code></pre>
  
  <p>I need to get the record where the <strong><em>latest</em></strong> action is 1</p>