【问题标题】:Laravel :: Combine two DB query objectsLaravel :: 组合两个数据库查询对象
【发布时间】:2014-10-23 22:18:11
【问题描述】:

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

别再纠结了,方法如下:

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 类型的对象用作数组

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

如果您仍然感到困惑(我不会怪您),请发表评论,我会解释更多。

编辑:

这是我使用->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;
}

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

【问题讨论】:

  • 在两列中都应该有一个唯一的 ID 才能使用 laravel merge

标签: php laravel


【解决方案1】:

merge()

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

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

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

【讨论】:

  • 因此,如果您要编辑此示例以匹配我的示例,结果将是 $data[$game-&gt;game]-&gt;merge($map),而 $map 从 $data[$game->game]['map'] 重命名跨度>
  • 我无法理解您要检索的内容。这个 merge() 只适用于 eloquent 的集合。
  • 两个查询都需要是雄辩的集合还是只能是一个?我有一个用于 Maps 表的模型,但没有用于统计数据库中的 games 表。
  • 是的,它们必须是一个雄辩的集合!让事情变得更简单而不是复杂。快乐的 laraveling :)
  • 必须有一种方法可以手动触发集合。我不能局限于 Eloquent。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-17
  • 2016-08-27
  • 1970-01-01
  • 2013-10-05
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
相关资源
最近更新 更多