title: Laravel :: Combine two DB query objects
body: <p>I have a method in my <em>Game</em> model that retrieves a list of all games registered in a table on one database, iterates through every game found and fetches data from another database, and finally grabs a random image URL from a database and appends it to the statistics object. Confused yet?</p>
  
  <p>Be confused no more, here's the method:</p>
  
  <pre class="lang-php prettyprint-override"><code>public static function getStats($player, $cache = 30) {
  
      // Return a collection of all games registered from the website database
  
      $games = Game::get(['game']);
  
      foreach($games as $game) {
  
          // For each iteration, return the statistics from the games database for that game
  
  
          // Grab game meta information from the website database, $game-&gt;game is the game name.
  
          $list = Game::where('game', $game-&gt;game)-&gt;remember($cache)-&gt;first();
  
  
          // From the game database, retrieve the statistics 
  
          $stats[$game-&gt;game] = DB::connection('game')-&gt;table($list-&gt;stats_table)
                                                      -&gt;select(DB::raw($list-&gt;statistics))
                                                      -&gt;where('Username', $player)
                                                      -&gt;remember($cache)
                                                      -&gt;first();
  
          // Grab a random map image from the game database
  
          $stats[$game-&gt;game]['map'] = DB::connection('game')-&gt;table('Maps')
                                                             -&gt;select('Image')
                                                             -&gt;orderBy(DB::raw('RAND()'))
                                                             -&gt;remember($cache)
                                                             -&gt;first();
  
  
      }
  
      // Finally, return the statistics from the game paired with a random map image from the game
  
      return $stats;
  
  }
  </code></pre>
  
  <p>I would like to append the map query onto $stats so it's all in one place, currently that method fails due to: <strong>Cannot use object of type stdClass as array</strong></p>
  
  <p>Without the maps, when looping on $stats, it is an array of game statistics with the key as the game name.</p>
  
  <p>If you're still confused (I won't blame you) then leave a comment and I'll explain more.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here's my attempt at using <code>-&gt;merge()</code>:</p>
  
  <pre><code>public static function getStats($player, $cache = 30) {
  
      // Return a collection of all games registered from the website database
  
      $games = Game::get(['game']);
  
      foreach($games as $game) {
  
          // For each iteration, return the statistics from the games database for that game
  
  
          // Grab game meta information from the website database
  
          $list = Game::where('game', $game-&gt;game)-&gt;remember($cache)-&gt;first();
  
  
          // From the game database, retrieve the statistics 
  
          $stats[$game-&gt;game] = DB::connection('game')-&gt;table($list-&gt;stats_table)
                                                      -&gt;select(DB::raw($list-&gt;statistics))
                                                      -&gt;where('Username', $player)
                                                      -&gt;remember($cache)
                                                      -&gt;first();
  
          // Grab a random map image from the game database
  
          $map = DB::connection('game')-&gt;table('Maps')
                                     -&gt;select('Image')
                                     -&gt;orderBy(DB::raw('RAND()'))
                                     -&gt;remember($cache)
                                     -&gt;first();
  
          $stats[$game-&gt;game] = $stats[$game-&gt;game]-&gt;merge($map);
  
  
  
      }
  
      // Finally, return the statistics from the game paired with a random map image from the game
  
      return $stats;
  }
  </code></pre>
  
  <p>Which results in <strong>Call to undefined method stdClass::merge()</strong> running Laravel 4.2.</p>