title: Laravel - whereHas checking latest record of a relationship without checking others
body: <p>I have two tables that has One to Many relationship.</p>
  
  <p>Bookings - <code>(id)</code></p>
  
  <p>booking_tasks - <code>(id, booking_id,user_id)</code></p>
  
  <p>one booking has many task
  one task has one booking</p>
  
  <p>Booking Model:</p>
  
  <pre><code> public function tasks() {
      return $this-&gt;hasMany('App\BookingTask');    
    }
  </code></pre>
  
  <p>BookingTask Model</p>
  
  <pre><code>public function booking() {
      return $this-&gt;belongsTo('App\Booking');
  } 
  </code></pre>
  
  <p>I want to get list of bookings that,
  user_id = 2 for latest booking_task for the bookings. I do not want to check other old booking_tasks of the bookings.</p>
  
  <p>ex:</p>
  
  <p><a href="https://i.stack.imgur.com/IH5ii.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IH5ii.png" alt="enter image description here"></a></p>
  
  <p>I want to check whether the last record of the booking_task's user_id=2 then get it as a booking.
  In the example last booking_task's user_id = 5. So it will not get as booking.</p>
  
  <p>My code is:</p>
  
  <pre><code>$bookings=Booking::whereHas('tasks',function($q){
              $q-&gt;where('user_id',2);//this will check whether any of record has user_id =2
  })-&gt;get();
  </code></pre>
  
  <p>I used this also:
  But it is not a correct one,</p>
  
  <pre><code>$bookings=Booking::whereHas('tasks',function($q){
                  $q-&gt;latest()-&gt;where('user_id',2)-&gt;limit(1);//this will check whether any of record has user_id =2 and return latest one.
      })-&gt;get();
  </code></pre>
  
  <p>Ho can I solve this problem, I have to use Laravel Eloquent also.</p>