title: Laravel 4 - Eloquent query scope order
body: <p>I'm currently having some troubles with Eloquent and how to order my query scopes.</p>
  
  <p>My goal here is to get pages with comments and pages stored in a session variable. On the top of that, I also have a filter system to get pages of specific categories. </p>
  
  <p>There are my two scopes:</p>
  
  <pre><code>public function scopeCategoryFilter($query, $filters = array())
  {
      return !(empty($filters)) ?  $query-&gt;whereIn('page_type_code', $filters) : $query;
  }
  
  
  public function scopeInSession($query, $pages = array())
  {
      return !empty($pages) ? $query-&gt;orWhereIn('code', $pages) : $query;
  }
  </code></pre>
  
  <p>And my Eloquent request:</p>
  
  <pre><code>$pages = Page::has('Comments')
      -&gt;InSession(Session::get('navigation.pagelist', []))
      -&gt;CategoryFilter($filters)
      -&gt;get();
  </code></pre>
  
  <p>I have no errors but the result is not what I expect.</p>
  
  <p>This query returns me : </p>
  
  <blockquote>
    <p>(Posts having Comments) <strong>OR</strong> (Posts in session with given
    Categories)</p>
  </blockquote>
  
  <p>And i would like to have :</p>
  
  <blockquote>
    <p>(Posts having Comments OR in session)->With given Categories.</p>
  </blockquote>
  
  <p>I think the problem is that the category filter is only apply on the pages returned by my <em>InSession</em> scope.</p>
  
  <p>So i found a solution:</p>
  
  <pre><code>$pages = Page::has('Comments')
      -&gt;CategoryFilter($filters)
      -&gt;InSession(Session::get('navigation.pagelist', []))
      -&gt;CategoryFilter($filters)
      -&gt;get();
  </code></pre>
  
  <p>But I don't like using my CategoryFilter scope twice.</p>
  
  <p>Do you have any idea how I could get the expected result without using my scope twice in the same Eloquent request? Maybe changing something in my scopes?</p>
  
  <p>Thank you!</p>