php 合并两个对象,php – Laravel ::结合两个DB查询对象

我在我的游戏模型中有一个方法,它检索在一个数据库中的表中注册的所有游戏的列表,遍历找到的每个游戏并从另一个数据库中获取数据,最后从数据库中抓取一个随机图像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,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().

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值