Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel :: Combine two DB query objects

Tags:

php

laravel

I have a method in my Game 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?

Be confused no more, here's the method:

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->game is the game name.

        $list = Game::where('game', $game->game)->remember($cache)->first();


        // From the game database, retrieve the statistics 

        $stats[$game->game] = DB::connection('game')->table($list->stats_table)
                                                    ->select(DB::raw($list->statistics))
                                                    ->where('Username', $player)
                                                    ->remember($cache)
                                                    ->first();

        // Grab a random map image from the game database

        $stats[$game->game]['map'] = DB::connection('game')->table('Maps')
                                                           ->select('Image')
                                                           ->orderBy(DB::raw('RAND()'))
                                                           ->remember($cache)
                                                           ->first();


    }

    // Finally, return the statistics from the game paired with a random map image from the game

    return $stats;

}

I would like to append the map query onto $stats so it's all in one place, currently that method fails due to: Cannot use object of type stdClass as array

Without the maps, when looping on $stats, it is an array of game statistics with the key as the game name.

If you're still confused (I won't blame you) then leave a comment and I'll explain more.

EDIT:

Here's my attempt at using ->merge():

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->game)->remember($cache)->first();


        // From the game database, retrieve the statistics 

        $stats[$game->game] = DB::connection('game')->table($list->stats_table)
                                                    ->select(DB::raw($list->statistics))
                                                    ->where('Username', $player)
                                                    ->remember($cache)
                                                    ->first();

        // Grab a random map image from the game database

        $map = DB::connection('game')->table('Maps')
                                   ->select('Image')
                                   ->orderBy(DB::raw('RAND()'))
                                   ->remember($cache)
                                   ->first();

        $stats[$game->game] = $stats[$game->game]->merge($map);



    }

    // Finally, return the statistics from the game paired with a random map image from the game

    return $stats;
}

Which results in Call to undefined method stdClass::merge() running Laravel 4.2.

like image 603
davidxd33 Avatar asked Aug 29 '14 20:08

davidxd33


1 Answers

merge()

The merge method merges the given array into the original collection. If a string key in the given array matches a string key in the original collection, the given array's value will overwrite the value in the original collection

The only parameter to the method is the collection that should be merged. Here's an example.

<?php

// app/routes.php

Route::get('/', function()
{
    $a = Album::where('artist','Something Corporate')
        ->get();
    $b = Album::where('artist','Matt Nathanson')
        ->get();

    $result = $a->merge($b);

    $result->each(function($album)
    {
        echo $album->title.'<br />';
    });
});

else

$array = array_merge($stats[$game->game]->toArray(), $map->toArray()); 
return Response::json($array);
like image 104
Ajay Kumar Ganesh Avatar answered Oct 15 '22 03:10

Ajay Kumar Ganesh