我目前卡在如何将两个查询结果合并到一个对象中。下面是我的代码。
已编辑
模型方法
public static function getTeamStats($competitionId, $teamId) {
return TeamCompetitionStatistics::where('competitionId', $competitionId)
->where('teamid', $teamId)
->where('periodNumber', 0)
->get();
}
public static function getTeamPosition($competitionId, $teamId){
return self::where('latest', 1)
->where('competitionId',$competitionId)
->where('competitorId', $teamId)
->get(['position', 'streak'])
->map(function($item, $key){
$item->position = $item->position . date("S", mktime(0, 0, 0, 0, $item->position, 0));
if(strpos($item->streak, '-') !== FALSE) {
$item->streak = str_replace('-', 'L', $item->streak);
}
else {
$item->streak = 'W'.$item->streak;
}
return $item;
});
}
在控制器中获取值
$teamStanding = Ladder::getTeamPosition($request->competitionId, $request->id);
$teamStatistics = TeamCompetitionStatistics::getTeamStats($request->competitionId, $request->id);
$result = $teamStatistics->merge($teamStanding);
返回结果: [{'teamstanding': 'data'}, {'teamstatictics': 'data'}]
预期输出: [{'teamstanding': 'data', 'teamstatictics': 'data'}]
原文由 PenAndPapers 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用 all() 函数。