10

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.

1
  • There should be a unique ID in both column to work with laravel merge Commented Mar 23, 2018 at 6:14

1 Answer 1

26

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);
Sign up to request clarification or add additional context in comments.

8 Comments

So if you were to edit this example to match mine, the outcome would be $data[$game->game]->merge($map) with $map being renamed from $data[$game->game]['map']
im not able to understand what you are trying to retrieve. this merge() is applicable only to eloquent collections.
Do both queries need to be eloquent collections or can it be just one? I have a model for the Maps table but not for the games table on the statistics database.
yes they have to be a eloquent collection! make things more simple rather than complicating. happy laraveling :)
There's must be a way I can manually trigger a collection. I can't be restricted to Eloquent.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.