title: orWhereHas - parameter grouping on Eloquent query - how do I do this in Laravel?
body: <p>In an eloquent query I am building, I am placing a constraint on a <code>has</code> relationship using Laravel 4.1's <code>whereHas</code> and <code>orWhereHas</code> methods.</p>
  
  <p>In the example soccer application, I wish to place a constraint on the <code>homeClub</code> and <code>awayClub</code> relationships so that I can the result set will select where the homeClub = Arsenal OR awayClub = Arsenal.</p>
  
  <p>This issue has evolved from an <a href="https://stackoverflow.com/questions/21335832/laravel-wherehas-on-multiple-relationships">earlier question</a> It seems that when using the <code>orWhereHas</code> method - the resulting sql doesn't group the <code>or</code> constraint.</p>
  
  <p>The query (relevant excerpt that is placing the constraints):</p>
  
  <pre><code>$ret
          -&gt;where( function( $subquery ) use ( $ret ){
              $ret-&gt;whereHas('homeClub', function ( $query ){
                  $query-&gt;where('name','Arsenal' );
              })-&gt;orWhereHas('awayClub',function ( $query ){
                  $query-&gt;where('name','Arsenal' );
              });
          })
          -&gt;where( function ( $subquery ) use ( $ret, $parameterValues ){
              $ret-&gt;whereHas('season', function ($query) use ( $parameterValues ){
                  $query-&gt;where('name', $parameterValues['season_names'] );
  
              });
          } )
          -&gt;whereHas('territory',function( $query ) use ( $parameterValues ){     
                 $query-&gt;where('region','Australia');
  
          })-&gt;get()-&gt;toArray();
  </code></pre>
  
  <p>This produces the sql:</p>
  
  <pre><code>SELECT * FROM `broadcasts` WHERE 
  
  (SELECT count(*) FROM `uploads` WHERE `broadcasts`.`upload_id` = `uploads`.`id` and `type` = 'international-audience') &gt;= '1' 
  and 
  (SELECT count(*) FROM `clubs` WHERE `clubs`.`id` = `broadcasts`.`home_club_id` and `name` = 'Arsenal') &gt;= '1' 
  or 
  (SELECT count(*) FROM `clubs` WHERE `clubs`.`id` = `broadcasts`.`away_club_id` and `name` = 'Arsenal') &gt;= '1' 
  and 
  (SELECT count(*) FROM `seasons` WHERE `broadcasts`.`season_id` = `seasons`.`id` and `name` = '2012/13') &gt;= '1' 
  and 
  (SELECT count(*) FROM `territories` WHERE `broadcasts`.`territory_id` = `territories`.`id` and `region` = 'Australia') &gt;= '1'
  </code></pre>
  
  <p>But, this isn't what I want, because referring to the eloquent statement, the club queries are  <a href="http://laravel.com/docs/queries#advanced-wheres" rel="noreferrer">grouped</a> and the query above either selects the homeClub constraints OR, the awayClub, season name, territory region. What I'm intending is the following SQL:</p>
  
  <pre><code>SELECT * FROM `broadcasts` WHERE 
  
  (SELECT count(*) FROM `uploads` WHERE `broadcasts`.`upload_id` = `uploads`.`id` and `type` = 'international-audience') &gt;= '1' 
  and 
  ((SELECT count(*) FROM `clubs` WHERE `clubs`.`id` = `broadcasts`.`home_club_id` and `name` = 'Arsenal') &gt;= '1' 
  or 
  (SELECT count(*) FROM `clubs` WHERE `clubs`.`id` = `broadcasts`.`away_club_id` and `name` = 'Arsenal') &gt;= '1' )
  and 
  (SELECT count(*) FROM `seasons` WHERE `broadcasts`.`season_id` = `seasons`.`id` and `name` = '2012/13') &gt;= '1' 
  and 
  (SELECT count(*) FROM `territories` WHERE `broadcasts`.`territory_id` = `territories`.`id` and `region` = 'Australia') &gt;= '1'
  </code></pre>
  
  <p>Note.. the parentheses on the club subquery.</p>
  
  <p>Does anyone know how I would write this as the eloquent query? I really don't want to have to revert to fluent / joins.</p>