title: Syntax error or access violation when query returns empty
body: <p>I am getting some weird error if the query returns nothing.  How can I handle this properly?</p>
  
  <p>Error:</p>
  
  <blockquote>
    <p>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 ')' at line 1 (SQL: select <code>users</code>.<code>uid</code>, <code>first_name</code>, <code>last_name</code>, IFNULL(files.filename, 'default.jpg') AS avatar, <code>privacy</code> from <code>users</code> left join <code>user_avatars</code> on <code>users</code>.<code>uid</code> = <code>user_avatars</code>.<code>uid</code> left join <code>files</code> on <code>user_avatars</code>.<code>fid</code> = <code>files</code>.<code>fid</code> where <code>users</code>.<code>uid</code> in ())</p>
  </blockquote>
  
  <p>Code:</p>
  
  <pre><code>$me = User::loggedin();
  $contacts = DB::table('contacts')
          -&gt;where('status', 'contact')
          -&gt;where('uid_by', $me)
          -&gt;orWhere('uid_to', $me)
          -&gt;select(
          DB::raw("IF(uid_by = $me, uid_to, uid_by) AS user_id")
          )
              -&gt;paginate(10);
  
  if ($contacts != null) {
      $users_id = array();
  
      foreach ($contacts as $contact) {
      $users_id[] = $contact-&gt;user_id;
  }
  
  $data = DB::table('users')
          -&gt;leftJoin('user_avatars', 'users.uid', '=', 'user_avatars.uid')
          -&gt;leftJoin('files', 'user_avatars.fid', '=', 'files.fid')
          -&gt;whereIn('users.uid', $users_id)
              -&gt;select('users.uid', 'first_name', 'last_name', DB::raw('IFNULL(files.filename, \'default.jpg\') AS avatar'), 'privacy')
              -&gt;get();
  
  
  return Response::json(array('contacts' =&gt; $data));          
  }
  
  //if no data was returned
  return Response::json(array('contacts' =&gt; 0));
  </code></pre>
  
  <hr>
  
  <p><strong>update</strong></p>
  
  <p>I tried adding <code>die(var_dump($contacts));</code> but it returning alot of laravel related objects.  </p>
  
  <p>On another note if I add ?page=1 then it returns 2 contacts SUCCESSFULLY.<br>
  If I add ?page=2 then it gives the error. (probably because there is no data) but I want to handle this properly.</p>