title: Laravel where on relationship object
body: <p>I'm developing a web API with Laravel 5.0 but I'm not sure about a specific query I'm trying to build.</p>
  
  <p>My classes are as follows:</p>
  
  <pre><code>class Event extends Model {
  
      protected $table = 'events';
      public $timestamps = false;
  
      public function participants()
      {
          return $this-&gt;hasMany('App\Participant', 'IDEvent', 'ID');
      }
  
      public function owner()
      {
          return $this-&gt;hasOne('App\User', 'ID', 'IDOwner');
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class Participant extends Model {
  
      protected $table = 'participants';
      public $timestamps = false;
  
      public function user()
      {
          return $this-&gt;belongTo('App\User', 'IDUser', 'ID');
      }
  
      public function event()
      {
          return $this-&gt;belongTo('App\Event', 'IDEvent', 'ID');
      }
  }
  </code></pre>
  
  <p>Now, I want to get all the events with a specific participant.
  I tried with:</p>
  
  <pre><code>Event::with('participants')-&gt;where('IDUser', 1)-&gt;get();
  </code></pre>
  
  <p>but the <code>where</code> condition is applied on the <code>Event</code> and not on its <code>Participants</code>. The following gives me an exception:</p>
  
  <pre><code>Participant::where('IDUser', 1)-&gt;event()-&gt;get();
  </code></pre>
  
  <p>I know that I can write this:</p>
  
  <pre><code>$list = Participant::where('IDUser', 1)-&gt;get();
  for($item in $list) {
     $event = $item-&gt;event;
     // ... other code ...
  }
  </code></pre>
  
  <p>but it doesn't seem very efficient to send so many queries to the server.</p>
  
  <p>What is the best way to perform a <code>where</code> through a model relationship using Laravel 5 and Eloquent?</p>