Laravel ::合并两个数据库查询对象 [英] Laravel :: Combine two DB query objects

查看:1193
本文介绍了Laravel ::合并两个数据库查询对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Game 模型中有一个方法,该方法可以检索在一个数据库中的表中注册的所有游戏的列表,遍历找到的每个游戏并从另一个数据库中获取数据,最后获取一个随机数.来自数据库的图像URL,并将其附加到统计对象.感到困惑了吗?

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?

不再困惑,这是方法:

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;

}

我想将地图查询附加到$ stats上,因此将其全部放在一个位置,当前该方法由于以下原因而失败:不能将stdClass类型的对象用作数组

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时,它是一系列游戏统计信息,键为游戏名称.

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.

这是我尝试使用->merge()的情况:

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

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

推荐答案

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

该方法的唯一参数是应合并的集合.这是一个例子.

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 />';
    });
});

其他

$array = array_merge($stats[$game->game]->toArray(), $map->toArray()); 
return Response::json($array);

这篇关于Laravel ::合并两个数据库查询对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆