php Laravel :: 合并两个数据库查询对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25576134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:53:44  来源:igfitidea点击:

Laravel :: Combine two DB query objects

phplaravel

提问by davidxd33

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

我的游戏模型中有一个方法可以检索在一个数据库的表中注册的所有游戏的列表,遍历找到的每个游戏并从另一个数据库中获取数据,最后从数据库中获取随机图像 URL 并将其附加到统计对象。还糊涂?

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

我想将地图查询附加到 $stats 上,以便将其全部放在一个地方,目前该方法失败的原因是:无法使用 stdClass 类型的对象作为数组

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

如果没有地图,在 $stats 上循环时,它是一个以游戏名称为键的游戏统计数据数组。

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():

这是我尝试使用->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.

这导致调用未定义的方法 stdClass::merge()运行 Laravel 4.2。

回答by Ajay Kumar Ganesh

merge()

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

merge 方法将给定的数组合并到原始集合中。如果给定数组中的字符串键与原始集合中的字符串键匹配,则给定数组的值将覆盖原始集合中的值

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);