title: Laravel scope with two relations
body: <p>I have a main table, "help_pages" which stores id's of either articles or videos. There are two other tables: "articles" and "videos."</p>
  
  <p>In the example below, $portalType is either 'reseller' or 'global' for the purposes of my application. All was well as long as I didn't have a flag on videos to be unpublished. Now I'm having trouble filtering the scope function by BOTH articles and videos that are published.</p>
  
  <p>This is in my models/HelpPage.php:</p>
  
  <pre><code>public function scopeByPortalType($query, $portalType) {
      return $query-&gt;where('portal_type', '=', $portalType)
      -&gt;leftjoin('articles', 'content_id', '=', 'articles.id')
      -&gt;where('articles.published', '=', '1');
  }
  </code></pre>
  
  <p>what I wish I could do is just add a second</p>
  
  <pre><code>-&gt;leftJoin('videos', 'content_id', '=', videos.id')
  -&gt;where('videos.published', '=', '0');
  </code></pre>
  
  <p>but this returns too many rows. I tried creating a temp table with a UNION for both articles and videos:</p>
  
  <pre><code>        CREATE TEMPORARY TABLE IF NOT EXISTS tmp_assets AS 
              (SELECT id, 'Article' AS content_type FROM articles WHERE published = 1)
          UNION
              (SELECT id, 'Video' AS content_type FROM videos WHERE published = 1)
  </code></pre>
  
  <p>and then joining that, but no dice. This might be a smaller deal than I'm making it, but I'm lost now!</p>
  
  <p>Laravel version 4.2</p>