title: Eloquent Filtering Collection with Eager Loading
body: <p>I have been looking online and can't seem to figure this out.
  I have two tables: Teams and TeamMembers.  A team can have one or more team members, so it is a one-to-many relationship.</p>
  <p>Here is the basic of the two tables:</p>
  <pre><code>   class Team extends Base\EloquentBase
         {
            // Relationship
            public function teamMembers()
            {
              return $this-&gt;hasMany('TeamClock\Eloquent\Models\TeamMember', 'team_id', 'team_id');
            }
         }
  
         class TeamMember extends Base\EloquentBase
         {
            // Relationship
            public function team()
            {
              return $this-&gt;belongsTo('TeamClock\Eloquent\Models\Team', 'team_id', 'team_id');
            
            // Scope
            public function scopeHasDailyPerDiem($q)
            {
              return $q-&gt;where('is_daily_per_diem', '&gt;', 0);
            }
         }
  </code></pre>
  <p>As you can see, I have a scope in the TeamMember class, and I want to get all teams with team members who have column &quot;is_daily_per_diem&quot; is greater than 0.</p>
  <p>I have a test controller, and I'm dumping out the results:</p>
  <pre><code>$teams = Team::has('teamMembers')-&gt;with(array('teamMembers' =&gt; function($query){
                  $query-&gt;hasDailyPerDiem();
          }))-&gt;get();
  </code></pre>
  <p>Here is what I'm getting when dumping out:</p>
  <pre><code>[0] =&gt; Array
          (
              [team_id] =&gt; 55
              [phase_id] =&gt; value
              [team_name] =&gt; value
              [team_member_quota] =&gt; value
              [team_member_overstaff] =&gt; value
              [published] =&gt; value
              [tools_required] =&gt; value
              [dress_code] =&gt; value
              [special_instructions] =&gt; value 
              [team_members] =&gt; Array
                  (
                  )
  
          )
  ...
  [41] =&gt; Array
          (
              [team_id] =&gt; 115
              [phase_id] =&gt; value
              [team_name] =&gt; value
              [team_member_quota] =&gt; value
              [team_member_overstaff] =&gt; 0
              [published] =&gt; 1
              [tools_required] =&gt; 
              [dress_code] =&gt; 
              [special_instructions] =&gt; 
              [team_members] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [team_member_id] =&gt; value
                              [team_id] =&gt; 115
                              [position_id] =&gt; value
                              [employee_id] =&gt; value
                              [team_auth_id] =&gt; 1
                              [progress_auth_id] =&gt; 2
                              [is_daily_per_diem] =&gt; 1
                              [daily_per_diem] =&gt; 60.00
                              [pay_type_id] =&gt; 1
                              [flat_rate] =&gt; 
                              [is_active] =&gt; 0
                              [per_diem_beg_date] =&gt; 
                              [per_diem_end_date] =&gt; 
                              [beg_shift_id] =&gt; 
                              [end_shift_id] =&gt; 
                              [assign_email_guid] =&gt; value
                          )
  
                  )
  
          )
  </code></pre>
  <p>As you can see, I'm getting a mixture of teams with team members(that is daily per diem &gt; 0) and teams with empty team members.</p>
  <p>Is there a way to eliminate teams with empty team members?</p>
  <p>I also tried this and other stuff in my test controller:</p>
  <pre><code>$teams = Team::whereHas('teamMembers', function($query){
              $query-&gt;hasDailyPerDiem();
          })-&gt;get(); 
  </code></pre>
  <p>but this is returning an array of all team members with &quot;is per daily diem&quot; greater than zero, but I'm not getting any team information:</p>
  <pre><code>[0] =&gt; Array
          (
              [team_id] =&gt; 55
              [phase_id] =&gt; value
              [team_name] =&gt; value
              [team_member_quota] =&gt; value
              [team_member_overstaff] =&gt; value
              [published] =&gt; value
              [tools_required] =&gt; value
              [dress_code] =&gt; value
              [special_instructions] =&gt; value
          )
  ...
   [36] =&gt; Array
          (
              [team_id] =&gt; 115
              [phase_id] =&gt; value
              [team_name] =&gt; value
              [team_member_quota] =&gt; value
              [team_member_overstaff] =&gt; value
              [published] =&gt; value
              [tools_required] =&gt; value
              [dress_code] =&gt; value
              [special_instructions] =&gt; value
          )
  </code></pre>
  <p>Any help is much appreciated!!!</p>