title: Laravel: Combine dynamic scope and with
body: <p>I am trying to eager load a dynamic scope in Laravel 4.2</p>
  
  <p>I have the following Eloquent class:</p>
  
  <pre><code>class Project extends Eloquent {
  
      protected $table = 'projects';
  
      public function subProjects() {
          return $this-&gt;hasMany('SubProject');
      }
  }
  </code></pre>
  
  <p>On the <code>Subproject</code> class, I have the following dynamic scope:</p>
  
  <pre><code>public function scopeForUserInPeriod($query, $user, $interval) {
    return $query-&gt;whereHas('hourRegistrations', function($query) use($user, $interval) {
      $query-&gt;where('user_id', $user-&gt;id)-&gt;whereBetween('date', [$interval-&gt;from, $interval-&gt;to]);
    });
  }
  </code></pre>
  
  <p>Now I would like to eagerload the scope when I load my projects, so I do something like.</p>
  
  <pre><code>Project::with('SubProjects.ForUserInPeriod', $user, $period);
  </code></pre>
  
  <p>Unfortunately, <code>with</code> only accepts a list of relations to load. I can't seem to find any indications whatsoever on how I am supposed to eagerload scopes that take arguments. :-(</p>