
不要再困惑了,这是方法:
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,return the statistics from the games database for that game // Grab game Meta information from the website database $List = Game::where('game',$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方法将给定数组合并到原始集合中.如果给定数组中的字符串键与原始集合中的字符串键匹配,则给定数组的值将覆盖原始集合中的值
该方法的唯一参数是应合并的集合.这是一个例子.
<?PHP// app/routes.PHPRoute::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);总结
以上是内存溢出为你收集整理的php – Laravel ::结合两个DB查询对象全部内容,希望文章能够帮你解决php – Laravel ::结合两个DB查询对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)