title: Laravel Multiple Unions
body: <p>I'm having an issue adding a query with multiple Unions the "laravel way".</p>
  
  <p>The I am trying to accomplish a query equivalent to the one generated by the following: </p>
  
  <pre><code>$ipsql = "";
  for ($n = 1; $n &lt; $total_networks; $n++) {
      $ipsql .= "(SELECT * FROM ip WHERE network = " . $n . " AND used = 0 LIMIT 5)
              UNION ALL";
  }
  if ($n == $total_networks) {
      $ipsql .= "(SELECT * FROM ip WHERE network = " . $n . " AND used = 0 LIMIT 3) ORDER BY ip_addr";
  }
  </code></pre>
  
  <p>I haven't found an option for unions with Eloquent so I was attempting to use the query builder for this particular section but I keep running into an issue while using the builders unionAll.</p>
  
  <p>Using this:</p>
  
  <pre><code>$ip_list = DB::table('ips')-&gt;where('network', '=', '0')-&gt;where('used', '=', '0')-&gt;limit(5);
          for($n = 1; $n &lt; $network_count; $n++){
              $ip_list = DB::table('ips')-&gt;where('network', '=', $n)-&gt;where('used', '=', '0')-&gt;limit(5)-&gt;unionAll($ip_list);
          }
          $ips = $ip_list-&gt;get();
  </code></pre>
  
  <p>I keep getting a MySQL syntax error:</p>
  
  <pre><code>     SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
       check the manual that corresponds to your MySQL server version for the right syntax to use near
       'union all ((select * from `ips` where `network` = ? and `used` = ? limit 5) unio' at line 1 
      (SQL:
           (select * from `ips` where `network` = 16 and `used` = 0 limit 5) union all ((select * from `ips`
           where `network` = 15 and `used` = 0 limit 5) union all ((select * from `ips` where `network` = 14
           and `used` = 0 limit 5) union all ((select * from `ips` where `network` = 13 and `used` = 0 limit 5)
           union all ((select * from `ips` where `network` = 12 and `used` = 0 limit 5) union all ((select *
           from `ips` where `network` = 11 and `used` = 0 limit 5) union all ((select * from `ips` where
           `network` = 10 and `used` = 0 limit 5) union all ((select * from `ips` where `network` = 9 and
           `used` = 0 limit 5) union all ((select * from `ips` where `network` = 8 and `used` = 0 limit 5)
   union all ((select * from `ips` where `network` = 7 and `used` = 0 limit 5) union all ((select * from
           `ips` where `network` = 6 and `used` = 0 limit 5) union all ((select * from `ips` where `network` =
           5 and `used` = 0 limit 5) union all ((select * from `ips` where `network` = 4 and `used` = 0 limit
           5) union all ((select * from `ips` where `network` = 3 and `used` = 0 limit 5) union all ((select *
           from `ips` where `network` = 2 and `used` = 0 limit 5) union all ((select * from `ips` where
           `network` = 1 and `used` = 0 limit 5) union all (select * from `ips` where `network` = 0 and `used`
           = 0 limit 5)))))))))))))))))
  </code></pre>
  
  <p>I can see from the error that its nesting each new union call which is creating the syntax issue.
  I tried accomplishing the same task with DB::raw but seem to be goofing that somewhere also. Is there a way to accomplish this thats better suited to laravel?
  Thanks for looking!</p>